Home » PowerShell » PowerShell Split String

PowerShell Split String

The Split () function in PowerShell is used to split the string into multiple substrings. The Split() function uses the specified delimiting characters to split a string and returns a string array that contains the substrings.

PowerShell String Split method uses whitespace as the default character for string break into multiple substrings. You can use string, character array, or string array to specify delimiting characters or strings.

In this article, we will discuss how to split a string using the Split() function in PowerShell.

PowerShell String Split

Split() function in PowerShell String split the delimited string into substrings.

Syntax

Split 
string[] Split(Params char[] separator) 
string[] Split(char[] separator, int count)
string[] Split(char[] separator,StringSplitOptions)
String[] Split(char[] separator, int count, StringSplitOptions)

string[] Split(string[] separator,StringSplitOptions)
String[] Split(string[] separator, int count, StringSplitOptions)

The Split () function in PowerShell has overload methods that allow you to use either a character array or string array or limit the number of substrings.

Parameters

separator: Specify a character to break the string.

count: Maximum number of elements expected in the string array.

Returns

String[]: An array that contains the substrings that are delimited by a separator.

PowerShell Split String on WhiteSpace

You can split the string into multiple substrings on the whitespace delimiter. The Split() function uses whitespace as the default delimiter and split string on space into a string array.

$website = "Shell Geek"

# Break string by blank space
$strArr = $website.Split()

# Get the type of $strArr
$strArr.GetType()

# Print the split strings array

Write-Host $strArr

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

We have used the Split() method over the $website to split a string into multiple substrings, whitespace is used as the default delimiter and stores the results array in the $strArr variable.

GetType() function is used to check the type of $strArr variable. The $strArr variable is of type String[] that contains the multiple substrings.

Using the Write-Host cmdlet, we print the $strArr that contains an array of strings.

The output of the above PowerShell script to split a string on space into an array is:

IsPublic IsSerial Name               BaseType                                                         
-------- -------- ----                             ---------                                                        
True     True     String[]                System.Array       
                                              
Shell 
Geek

PowerShell Split String on First Space

Use the PowerShell split operator to split a string on the first space. It splits the string that matches the first white space in the given string and returns a substring.

$my_text = "Hello Welcome to ShellGeek"  

# Split the string on first space
$my_text -split ' ',2  

In the above PowerShell script, the $my_text variable stores the string data. To split the string on the first space, we used the split operator.

The output of the above PowerShell script to split a string on the first space delimiter returns the remaining part of the string as a substring.

PS C:\> $my_text = "Hello Welcome to ShellGeek"                                                                         

PS C:\> $my_text -split ' ',2                                                                                           
Hello
Welcome to ShellGeek

PS C:\>  

PowerShell Break the String by Comma

You can use the comma (,) as a delimiter to split a string into multiple substrings.

$testString = "Hello,Welcome,to,Shell Geek"

# Split string by comma as single delimiter
$testString.Split(',')

In the above PowerShell script, the $testString variable stores the string. We have used comma (,) as a delimiter to break the string into multiple substrings.

The output of the above PowerShell script that uses a comma to split a string is:

Hello
Welcome
to
Shell Geek

Conclusion

I hope the above article on how to use the Split() function in PowerShell to split the string is helpful to you.

The Split() function breaks the string and returns the string array. Delimiters are not included in the returned array.

If the separator is null or not specified in the Split() function, it takes white space as the default delimiter.

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

Recommended Content

Using PowerShell to Convert String to Array

PowerShell Split on Newline

PowerShell Split String into Variables

PowerShell Split file into Smaller files

PowerShell Split by Multiple Characters

PowerShell Split Regex

PowerShell Split to Get Last Element

PowerShell Split a String on the First Occurrences of Substring

PowerShell Split Path into Array