PowerShell String Manipulation
When working with a string of characters, PowerShell has a number of built in methods that allow for them to be easily manipulated.
When working with the console, a string can be displayed using the ‘Write-Host’ cmdlet.
Write-Host "Hello world!"
If it is necessary to incorporate the value of a variable into this message it can be done in a number of different ways.
$fname = "Fred" $sname = "Bloggs" # Variables directly in a string. Write-Host "Hello $fname $sname!" # Plus symbol used to concatenate a string. $message = "Hello " + $fname + " " + $sname + "!" Write-Host $message # .NET string format string. $message = [string]::Format("Hello {0} {1}!", $fname, $sname) Write-Host $message # PowerShell format string. $message = 'Hello {0} {1}!' -f $fname, $sname Write-Host $message
All four of the above examples display the same message in the console, 'Hello Fred Bloggs!'.
Variables in PowerShell have their own properties and methods, for example, string variables have a method to convert them to upper case. In order to utilise these in a larger string, the variable needs to be enclosed in brackets, with a dollar sign before the opening bracket.
$fname = "Fred"
$sname = "Bloggs"
Write-Host "Hello $($fname.ToUpper()) $($sname.ToUpper())!"
Here, the first and last name variables are converted to upper case using the 'ToUpper' method, to produce a message, 'Hello FRED BLOGGS!'.
Similarly, in order to run commands as part of a string, they also need to be enclosed in brackets and preceded by a dollar sign.
Write-Host "Today's date is: $(Get-Date -Format 'dd/MM/yyyy')."
The above example displays the current date as part of a string.
Today's date is: 27/08/2023.
Index Value
Every character in a string has an index value, which can be used to reference it. The first character has an index value of zero, the second has an index of one and so on.
$example = "This is a string."
Write-Host $example[3]
Here, the index value of three in square brackets is used to output the fourth character of the string to the console.
It is also possible to find the index position of a character in a string using the ‘IndexOf’ method.
Write-Host $example.IndexOf("i")
Note that, the ‘IndexOf’ method will return the index position of the first occurrence of a character, if more than one exists in the string. There is also a ‘LastIndexOf’ method that can be used to return the index position of the last occurrence of a particular character.
Write-Host $example.LastIndexOf("i")
If the character does not exist in the string, then a ‘-1’ is returned.
Substring
Using the ‘Substring’ method, it is possible to return a portion of a string by specifying the index position to start from, together with the number of characters that are required.
$example = "This is a string."
Write-Host $example.Substring(5,4)
The above example starts at index position five and displays four characters.
is a
If only the index position to start from is specified, without the number of characters to include, then the whole of the rest of the string is shown.
Write-Host $example.Substring(5)
The result from this is shown below.
is a string.
Upper and Lower Case
In order to change a string to upper or lower case, there are two methods that can be used, ‘ToUpper’ and ‘ToLower’.
$example = "This is a string." Write-Host $example.ToUpper() Write-Host $example.ToLower()
The first will change the whole string to upper case and the latter will convert it to lower case.
THIS IS A STRING. this is a string.
Trim
If a string has extra spacing at the start, end, or both, it’s possible to remove this using the ‘Trim’ method as demonstrated below. If it is only required to remove the extra spacing from the start or end, then the 'TrimStart', or 'TrimEnd' methods can be used.
$example = " This is a string. " Write-Host $example.Trim() Write-Host $example.TrimStart() Write-Host $example.TrimEnd()
Length
Sometimes it is necessary to find the size or length of a string. This can be done using the ‘Length’ property.
$example = "This is a string."
Write-Host $example.Length
Insert, Remove and Replace
In order to manipulate the contents of a string the ‘Insert’, ‘Remove’ and ‘Replace’ methods can be used.
The ‘Insert’ method can be used to insert a string, into an existing string at the specified index position.
$example = "This is a string."
Write-Host $example.Insert(9, " short")
Here a space and the word ‘short’ is inserted into the string at index position nine, just after the ‘a’, to produce the string, ‘This is a short string.’.
The ‘Remove’ method can be used to delete a part of a string, by specifying the index position to start the deletion from, as well as the number of characters to delete. Note that, if the number of characters to delete is omitted then all characters from the index position to start from, to the end of the string, will be removed.
$example = "This is a short string."
Write-Host $example.Remove(9, 6)
In the above example, the deletion starts at index position nine, just after the ‘a’, and removes six characters, to produce the string, ‘This is a string.’.
Finally, the ‘Replace’ method can be used to replace a specified part of a string with some other text.
$example = "This is a short string."
Write-Host $example.Replace("short", "small")
Here, the word ‘short’ is replaced with the word ‘small’ to produce the string, ‘This is a small string.’.
The properties and methods discussed above are by no means exhaustive. There are many others available.