In this tutorial, we learn how to use different string manipulation methods used in PowerShell. Using these methods string in PowerShell can be converted or changed to the desired output. Below is the list of all string methods used by PowerShell.
The string is one of the datatypes used in PowerShell it is nothing but a sequence of characters written in double-quotes. In PowerShell string represent an object of the System.String. Using String in PowerShell there are different string manipulation operations that can be performed.
Example of string
“This is a test string”
How to declare a string
Declaration of the string is very simple to write anything inside double quotes is referred to as a string and the variable that contains that string is called a string variable. There are multiple ways to declare a string in PowerShell.
Example –
PS C :\> $var = "Hello World"
Here in this example variable, $var contains a string called “Hello World” and this variable is referred to as a string variable. To check the type of any PowerShell variable use the gettype() method of PowerShell cmdlet.
There is one more method to declare a string in PowerShell that is called an explicit declaration. Write datatype before the variable name this type of declaration is generally used in the advance function to avoid any type of exception.
PS C :\> [string]$var = "Hello World"
Types of Operation with string in PowerShell
There are different operations that can be performed with string in PowerShell. To get the list of all string operations uses the below command.
PS C :\> $var | Get-Member
Get-Member PowerShell cmdlet is used to display all string operations of variable $var.
Below is the list of operations which commonly used with string in PowerShell –
1.Concatenates String in PowerShell
Joining two or more strings is referred to as a concatenation of string.
Example –
PS C :\>$var1 = "Hello" PS C :\>$var2 = "World" PS C :\> $var1 + $var2
Example –
Now use the Concat Method to concatenate two strings.
PS C :\> $var1 = "Hello" PS C :\> $var2 = "World" PS C :\> [System.String]::Concat($var1,$var2)
Another method to join the string is the “join” method. Use can you any character or string in the join method to join the two or more strings.
Example –
PS C :\>$var1 = “Hello” PS C :\>$var2 = ”World” PS C :\> $var1,$var –join("-geeks-")
2. String Upper and Lower Case in PowerShell
To convert any string in upper or lower case use the “.ToUpper()” method for upper case and “.ToLower()” method for lower case.
PS C :\>$var = "Geeks Tutorials" PS C :\>$var.ToUpper() PS C:\>$var.ToLower()
3.Split String in PowerShell
The split method is used to split the string into multiple lines based on split character. Based on the requirement choose any split character.
Here in the below example, I split the string-based on white space. Enter one space in split opening and closing braces.
Example –
PS C:\>$var = "I am learning PowerShell from Geeks Tutorials" PS C :\> $var.split( )
3.Contains Operation on String PowerShell
Contains method check whether the particular word exists in a string or not. It returns the Boolean value either true or false.
PS C:\>$var = "I am learning PowerShell from Geeks Tutorials" PS C:\>$var.Contains("Tutorials") PS C:\>$var.Contains("tutorials")
Here it returns true and false for the same word because this method is case sensitive.
4.Trim on String in PowerShell
When the trim() method is used on the string it removes white space or any character from the beginning and end of the string. By default, it removes white space.
Example 1:
It removes white-spaces from the beginning and end of the string.
PS C:\>$var = " Geeks Tutorials " PS C:\>$var.trim()
Example 2:
Now remove a specific character from the string from beginning and end. Suppose I have to need to remove “G” so it only removes from the beginning because “G” doesn’t exist at the end.
PS C:\>$var = “Geeks Tutorials” PS C:\>$var.trim(‘G’)
Example 3:
Now I need to remove character “t” from the string but “t” neither exists in beginning nor exists on the end hence this does not remove from the string.
PS C:\>$var = "Geeks Tutorials" PS C:\>$var.trim('t')
Note: In example 3 the trim method does not remove because character ‘t’ doesn’t exist in string or end of the string hence anything between the string doesn’t trim.
There are two more methods of trim such as TrimStart() and TrimEnd() which remove characters from the starting and end of the string respectively.
5.Replace String in PowerShell
To replace a particular character or word from the string replace method is used. It takes two arguments first which character or word needs to be replaced and the second new character or word that replaces the existing one.
PS C:\>$var = "Geeks Tutorials" PS C:\>$var.Replace("Geeks", "Hi Geeks")
Word “Geeks” replaced with “Hi Geeks” in the string.
6.Get Length of String in PowerShell
.Length function is used to get the length of the string.
PS C:\>$var = "I start learning PowerShell" PS C:\>$var.Length
7.Substring in PowerShell
To extract some part of the string the substring method is used in PowerShell. It works on the index and the index starts from 0.
PS C:\>$var = "Welcome to Geeks Tutorials" PS C:\>$var.SubString(0,15)
It extracts 0 to 15 characters from the start of the string. Here 0 is the first index and 15 is the last index of the extracted string.
To extract the complete string pass string length in the substring method.
PS C:\>$var = “Welcome to Geeks Tutorials” PS C:\>$var.SubString(0, $var.Length)
8.Indexof and LastIndexof in the string using Powershell
Indexof method is used to find the position of the particular character in the given string. It finds the first occurrence of the character in the string.
The LastIndexof method finds the last occurrence of the character in the string.
Example 1:
PS C:\> $var = "This is a powershell tutorial" PS C:\> $var.Indexof(“s”)
Example 2:
PS C:\> $var = "This is a powershell tutorial" PS C:\> $var.LastIndexof(“s”)
9.Compare string in PowerShell
CompareTo method is used to compare two strings in PowerShell. This is case sensitive it returns 0 if the string is identical and returns 1 if the strings don’t match.
PS C:\> $var1 = "Geeks Tutorials" PS C:\> $var2 = "geeks tutorials" PS C:\> $var1.CompareTo($var2) PS C:\> $var1.CompareTo("Geeks Tutorials")
10.Convert object into a string
To convert any object into a string using the ToString() method in PowerShell. It converts the object or any other variable to string.
Example 1:
In this example, an integer value is converted into a string but the datatype is still an integer.
PS C:\> $varInt = 555 PS C:\> $varInt.ToString() PS C:\> $varInt.GetType()
Now, in the below example, I have converted the DateTime value into a string.
Example 2:
PS C:\> $date = Date PS C:\> $date
Output – Tuesday, October 27, 2020 10:22:13 AM
PS C:\> $date.ToString() 10/27/2020 10:22:13 AM PS C:\> $date.GetType()
Conclusion
String Operations is commonly used in PowerShell scripting because it is easy use in long script and operations. In PowerShell, most outputs are in String format or can be converted into a string. There is another Pipeline cmdlet Select-String which effectively works on string operation when there is a large extract of data or files through the Get-Content or XML.