PowerShell substring extracts a part of the string. The substring returns the part of the string between the start and the specified number of characters.
substring(startIndex) substring(startIndex, int length)
startIndex – Zero-based starting character of the string.
length – number of characters to return.
In this article, we will discuss how to use the PowerShell substring method to extract the substring from the given PowerShell string and get the first character of a string.
PowerShell Get First Character of String
Use the PowerShell Substring() method to get the first character of a string.
Let’s consider, the string ShellGeek and we want to get the first character of a string.
Use the Substring method over the given string and pass the start index as 0 to the start point of the character and length as 1 to get a number of characters to return.
The output of the above script gets the first character of a string.
PS C:\> "ShellGeek".Substring(0,1) S PS C:\>
Extracts Substring from String starts at location
Use the Substring() method over the string to extract the substring from the given string specified by
Let’s say, we have a string ShellGeek and we want to return the Geek word from it.
To retrieve the substring Geek from the given string, specify the start point of the substring by startIndex, 5.
It will start from character 5 and extracts all the characters from the given string. In the original string ShellGeek, the position of character G starts from 5 ( as it is zero-based indexing)
The output of the above PowerShell script after extracting the substring is:
Get First 5 Characters of String in PowerShell
Use the Substring() method to get the first 5 characters of the string in PowerShell.
Let’s say, we have a string ShellGeek and we want to return the first 5 characters as Shell from the string or return the left string of 5 characters.
Substring() method over the string ShellGeek and pass the start point of the substring as startIndex 0 and length as 5 to get the first 5 characters from the left in a string.
The output of the above PowerShell script retrieves the first five characters of the string or returns the left 5 characters of a string.
Get the Last 4 Characters as a Substring in PowerShell
To get the last 4 characters from a string, get the length of the given string and subtracts 4 from it to get the last 4 characters in the string from the right as a substring.
Let’s say, we have a string that contains the URL https://192.168.0.1:5001. We want to get the port number used in the URL.
To get the port number which is of 4-character length, find the string length of the URL. Use the Length property over the string to get the length of a string.
Subtract 4 from the length to get the 4 characters from the right, port number 5001 in the example.
$url = "https://192.168.0.1:5001" $url.Substring($url.Length-4)
The output of the above PowerShell script retrieves the last 4 characters from the string as a substring.
Cool Tip: How to use the PowerShell String Split method to split the string!
I hope the above article on how to use the PowerShell substring method to retrieve the part of the string is helpful to you.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.
PowerShell Substring IndexOf – Find the position of the substring.
PowerShell Substring After Character – Extracts the Substring after a character.
String Length of Variable – Get the string length of a variable in PowerShell.