Updated June 28, 2023
Introduction to String in PowerShell
A string is considered a sequence of characters and is one of the datatypes in PowerShell. Its object type is System. String. When we mention string, everything in a double quote or single quote is considered a string; the only difference between them is a representation of a variable inside the string. In PowerShell, nearly every output can be converted into String objects.
How to Declare a String in PowerShell?
In PowerShell, anything you mentioned inside the double quote or single quote is considered a string; only the variable representation is different in both cases inside the string. There are a few methods with which you can declare a string. First, you can directly assign a string to a variable, and the variable’s data type becomes a string. For example, Here, $var is now a string variable. You can check its datatype.
Code:
$var = "This is a string"
$var.GetType()
Output:
The second method to declare a string is explicit. Here you are converting a variable to a string. The below method is generally used in function and advanced functions while declaring parameters.
Code:
[String]$var1 = "This is a string"
Operations Performed with String in PowerShell
To check which operations PowerShell can perform on a string, you must pipeline the Get-Member command and check for the methods.
Code:
$var | Get-Member
We will discuss here one by one operation.
1. To check the Length of the String
You can check the string length or the number of characters in the string you need to use the Length function of the string.
Code:
$var = "This is a test string"
$var.Length
Output:
2. Split Operation on a String
Applying a split operation on a string divides the string into multiple lines from where the split character is mentioned. Let’s take our environment PS module path example,
Code:
$env:PSModulePath
Output:
We want to split the path in the above output.
Code:
($env:PSModulePath).Split(';')
Output:
Similarly, you can divide the string into the desired output using different split characters.
3. Join operation on a String
When you use the Join operation on a string, it concatenates two strings.
Code:
"Hello" + " World"
Output:
Another method you can use is the Join command.
Code:
$a = "Hello"
$b = "World"
$a,$b -join(' ')
Output:
If you want to Join two strings with a ‘#‘ character, put the ‘#’ inside the Join function.
Code:
$a = "Hello"
$b = "World"
$a,$b -join('#')
Output:
You can join more than two strings as well.
Code:
$a = "Hello"
$b = "World"
$c = "Delta"
$a,$b,$c -join('#')
Output:
You can also join the string with the Concat method.
Code:
$a = "Hello"
$b = "World"
$c = "Delta"
[System.String]::Concat($a,$b,$c)
Output:
4. SubString Operation on a String
A substring command in a string is used to extract the part of the string. It works on indexing. In a string, the index starts from 0.
Code:
$var = "This is a string"
$var.Substring(0,5)
Output:
To extract the entire string,
Code:
$var = "This is a string"
$var.Substring(0,$var.Length)
Output:
5. Contains Operation on a String
Contain function, when applied to a string, checks whether the specific word exists in the string. This function is case-sensitive. The output will be in a Boolean (TRUE / FALSE) format.
Code:
$var = "This is a string"
$var.Contains("this")
$var.Contains("This")
The output will be False and True, respectively. As this method is case sensitive, when a function matches “this,” it can’t recognize part in the string.
Output:
You can also validate the part of the word in a string.
Code:
$var = "This is a string"
$var.Contains("ring")
The output will be True.
6. Upper Case and Lower Case Operation on a String
You can convert the entire string into the Upper or Lower cases.
Code:
$var = "This is a string"
$var.ToUpper()
$var.ToLower()
Output:
7. Trim Operation on a String
Trim operation in PowerShell is used to trim or remove the space or specific characters from the string’s beginning and end. For example,
Using only the Trim() function on the string will remove the space from the beginning and the end of the string. This operation is case-sensitive when you use any character inside the function.
Code:
(" Hello World ").Trim()
Output:
To trim the specific characters from both ends,
Code:
("Hello World").Trim('H') #Removes the 'H' from the beginning
("Hello World").Trim('Hd') #Removes the 'H' from the beginning and 'd' from the end.
("Hello World").Trim('e') #Removes nothing as 'e' is not beginning or the end of the string
Output:
TrimStart() and TrimEnd() functions also remove the space or the specific character from the beginning and the end of the string, respectively.
TrimStart() example,
Code:
("Hi, this ends with H").TrimStart('H')
Output:
TrimEnd() example,
Code:
("Sir, this ends with S").TrimEnd('S')
Output:
When you mention multiple characters together, the operation removes them.
Code:
("Hello World").TrimStart("Hel")
("Hello World").TrimEnd("ld")
Output:
8. Replace operation on a String
A Replace operation is available on a string command to replace the specific character or set of characters. This operation is not case-sensitive. The below command will replace ‘N’ with a null character.
Code:
("New PowerShell version is 7.0") -Replace ("N","")
Output:
You can replace the space with a specific character.
Code:
("New PowerShell version is 7.0") -Replace (' ','#')
Output:
To replace the word as well,
Code:
("New PowerShell version is 7.0") -Replace ('PowerShell','Python')
Output:
9. Index Operation on a String
Index operation in a PowerShell is used to find the position of the particular character in the string. Indexing starts at 0. IndexOf() operation finds the first occurrence of the character.
Code:
("New PowerShell version is 7.0").IndexOf('P')
Output:
LastIndexOf() finds the last occurrence of the character.
Code:
("New PowerShell version is 7.0").LastIndexOf('n')
Output:
10. Converting an Object into a String
To convert any object into a string data type, use ToString() command. In the below example, we will convert an integer value to a string.
Code:
$intvar = 12345
$intvar.GetType()
If you check the datatype of the $intvar, it will be the Integer type.
Output:
To convert the above variable into a string,
Code:
$intvar = 12345
$intvar = $intvar.ToString()
$intvar.GetType()
Output:
Conclusion
String operation is frequently used in PowerShell scripting as it is easy to perform an operation on the number of lines and extract the data. In PowerShell, most outputs are in String format or can be converted into a string. Another Pipeline cmdlet Select-String, effectively works on string operation when there is extensive data extraction through the Get-Content or XML.
Recommended Articles
This has been a guide to String in PowerShell. Here we discuss the introduction, operations, and how to declare a string in PowerShell. You may also have a look at the following articles to learn more –