Home » PowerShell » PowerShell String – Complete Guide

PowerShell String – Complete Guide

PowerShell string is of data type String and BaseType as System.Object. String in PowerShell are objects and can use available string methods for string manipulation.

PS C:\> "ShellGeek".GetType()         
                                                                                  
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


PS C:\>   

PowerShell string can be defined using single or double quotes and are of System.String type.

In this article, we will discuss strings in PowerShell, get members for strings, and string manipulation using different string methods.

Strings in PowerShell

Strings in PowerShell can be defined using single or double quotes. You can also store the string in the variable. Strings created are System.String data type.

PS C:\> 'ShellGeek'.GetType()                                                                                           
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


PS C:\> 'ShellGeek'.Length                                                                                              9
PS C:\> "ShellGeek".GetType()                                                                                           
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


PS C:\> "ShellGeek".Length                                                                                              9
PS C:\> $website = "ShellGeek"                                                                                          PS C:\> $website.GetType()                                                                                              
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


PS C:\> $website.Length                                                                                                 9
PS C:\>      

In the above PowerShell script, we have defined strings in PowerShell using different ways like single quotes, double quotes, or storing strings in a variable.

Using the GetType() method, we get the data type and base type for the object. All strings are of type System.String data type.

The Length is string property is used to get the length of a string.

Get String Members in PowerShell

PowerShell string are object and has members to manipulate the string.

You can get all the members of the PowerShell string using the Get-Member cmdlet. It displays the name, type, and description.

"ShellGeek" | Get-Member | Select Name, MemberType     

The output of the above PowerShell script gets all the members for the string including methods and property names available for the string.

PS C:\> "ShellGeek" | Get-Member | Select Name, MemberType                                                              
Name                             MemberType
----                                   ----------
Clone                           Method
CompareTo                       Method
Contains                        Method
CopyTo                          Method
EndsWith                        Method
Equals                          Method
GetEnumerator                   Method
GetHashCode                     Method
GetType                         Method
GetTypeCode                     Method
IndexOf                         Method
IndexOfAny                      Method
Insert                          Method
IsNormalized                    Method
LastIndexOf                     Method
LastIndexOfAny                  Method
Normalize                       Method
PadLeft                         Method
PadRight                        Method
Remove                          Method
Replace                         Method
Split                           Method
StartsWith                      Method
Substring                       Method
ToBoolean                       Method
ToByte                          Method
ToChar                          Method
ToCharArray                     Method
ToDateTime                      Method
ToDecimal                       Method
ToDouble                        Method
ToInt16                         Method
ToInt32                         Method
ToInt64                         Method
ToLower                         Method
ToLowerInvariant                Method
ToSByte                         Method
ToSingle                        Method
ToString                        Method
ToType                          Method
ToUInt16                        Method
ToUInt32                        Method
ToUInt64                        Method
ToUpper                         Method
ToUpperInvariant                Method
Trim                            Method
TrimEnd                         Method
TrimStart                       Method
Chars            ParameterizedProperty
Length                        Property


PS C:\>                                                                                                                                                              

Concatenation of String

You can concatenate multiple strings in PowerShell to build formatted strings using the plus (+) operator.

PS C:\> $website = "ShellGeek"                                                                                          PS C:\> $category = "PowerShell"                                                                                        PS C:\> $website + " " + $category                                                                                                                        

In the above PowerShell script, we have defined two strings and stored them in variables. To concatenate strings in PowerShell, we have used the + operator.

The output of the above script for concatenation of strings is:

ShellGeek PowerShell

Use the Join parameter to Join the String in PowerShell

If you want to join multiple strings in PowerShell, you can use the join operator and specify the string you want to concatenate.

PS C:\> $website = "ShellGeek"                                                                                          PS C:\> $category = "PowerShell"                                                                                        PS C:\> $website,$category -join " - Learn "  

In the above PowerShell script, we have defined two variables holding string data types. To perform the join operation, used the join operator and specified the string ” – Learn “.

The output for the above script to join the strings in PowerShell is:

ShellGeek - Learn PowerShell

Variable Substitution for String in PowerShell

In PowerShell, you can store strings in the variable. To concatenate the strings in PowerShell, you can specify variables directly in the strings.

You will need to use double quotes around the strings for the concatenation of strings.

PS C:\> $website = "ShellGeek"                                                                                          PS C:\> $category = "PowerShell"                                                                                        PS C:\> "Hello! Welcome to $website - $category website"   

In the above PowerShell script, we have defined two strings variables $website and $category.

To perform the concatenation of strings, we have used double quotes around the strings. The double-quoted string allows the variable substitution.

The output of the above script for variable substitution in the double-quoted string is:

Hello! Welcome to ShellGeek - PowerShell website

Format the String in PowerShell

Using the .NET static method Format for string, you can easily format the string in PowerShell. You can also format the string in PowerShell using the -f

# Format string in PowerShell using .NET String Format
$website = "ShellGeek"                                                                                        $category = "PowerShell"                                                                                        [string]::Format('Hello! Welcome to, {0} {1}.',$website,$category) 

# Format string in PowerShell using -f
 'Hello! Welcome to, {0} {1}.' -f $website,$category      

In the above PowerShell script, we have defined string variables. Using the .NET string method Format, it formats the string.

In the script, the string is parsed for the tokens {0} and {1} and used the values provided.

The output of the above script to format the string using .NET and PowerShell is:

Hello! Welcome to, ShellGeek PowerShell.

Hello! Welcome to, ShellGeek PowerShell.

Split the Strings in PowerShell

You can split the string in PowerShell using the split() method or split operator. You can specify the string or character array to split the string. It returns an array of strings.

$website = "Shell Geek"  
# Split the string using Split() method
$website.Split(" ")
 

In the above PowerShell script, the $website variable contains the string “Shell Geek”.

To split the string by white space, we have used the Split() operator and specified white space as an input argument to it.

The output of the above script after splitting the string returns the array of strings.

Shell
Geek

You can also use the split operator to split the string. Refer to the following code which uses the split operator and splits the string by whitespace.

$website = "Shell Geek"  

# Split the string using split operator

$website -split " "    

The output of the above script after splitting the string by the split operator is:

Shell
Geek

Substring in PowerShell

The substring method in PowerShell takes two arguments as input and returns the part of the string. These input parameters are numeric and separated by a comma.

$website = "Shell Geek"

$website.Substring(0,6) 

$website.Substring(6,4)

In the above PowerShell script, the $website variable contains the string “Shell Geek”.

We have used the substring method that takes two input numeric values and returns the part of the string.

In the following command, the Substring method takes values as 0 and 6 and returns the first 6 characters from the given string.

$website.Substring(0,6) 

The output of the above script is:

Shell

In the next command, the Substring method takes values 6 and 4. It skips the first six characters and returns the next 4 characters from the given string.

$website.Substring(6,4)

The output of the above script is:

Geek

Replace() in PowerShell

The Replace() method of string in PowerShell takes two parameters and returns the part of the string.

$website = "Shell Geek"
$website.Replace(" ","")

In the above PowerShell script, the $website variable stores the string type value.

We have used Replace() method on the string variable to replace the white space from the given string with empty and returns the string.

The output of the above script after replacing the whitespace in the given string is:

ShellGeek

Conclusion

I hope the above article on strings in PowerShell is helpful to you. There are a number of string methods you can use to manipulate the strings in PowerShell.

Use the Get-Member cmdlet to get the list of all available members ( methods and properties).

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Recommended Content

PowerShell Split String

PowerShell Replace String or Character

Leave a Comment