Introduction to String in PowerShell
A string is considered as a sequence of characters and it 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 as a string, the only difference between them is a representation of 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 as a string only the variable representation is different in both the cases inside the string. There are few methods with which you can declare a string. First is 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 an explicit method. Here you are converting variable to 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 that PowerShell can perform on a string, you need to 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
When you apply a split operation on a string it divides the string into multiple lines from where the split character is mentioned. Let 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 use different split characters to divide the string into the desired output.
3. Join Operation on a String
When you use Join operation on a string, it actually concatenates two strings.
Code:
"Hello" + " World"
Output:
Another method you can use the Join command.
Code:
$a = "Hello"
$b = "World"
$a,$b -join(' ')
Output:
If you want to Join two strings with ‘#’ character then simply 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
Substring command in a string is used to extract the part of the string. It totally 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 on a string, it checks if the specific word exists in the string or not. 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 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 case or the Lower case.
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 the specific characters from the beginning and the end of the string. For example,
When you use only Trim() function on the string then it 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:
There are also TrimStart() and TrimEnd() functions, which 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
To replace the specific character or set of characters, there is a Replace operation available on a string command. 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 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, you need to 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. There is another Pipeline cmdlet Select-String which effectively works on string operation when there is a large extract of data 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 –
- PowerShell Import Module
- PowerShell String Functions
- PowerShell Invoke-Command
- Guide to Python Import Module
- PowerShell Convert to String | Examples
- Guide to PowerShell String Replace
2 Online Courses | 1 Hands-on Project | 4+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses