Home » PowerShell » PowerShell Split String on First Occurrence of Substring

PowerShell Split String on First Occurrence of Substring

Using the PowerShell String built-in Split() method or split operator, you can split a string on the first occurrence of a substring or character.

Using the split operator in PowerShell, it can return the maximum number of substrings based on the pattern. PowerShell String Split() method also split the string based on a specified substring or character and returns multiple substrings.

In this article, we will discuss how to use the PowerShell string Split() method and split operator to split a string on the first occurrence of a substring or character.

PowerShell Split() – Split String on First Occurrence of Substring

Use the PowerShell string Split() method to split the string on the first occurrence of substring and returns the first occurrence substring and second substring after the split.

$sysdate = "01-08-2022"

# Split the string on First Occurrence of substring
$sysdate.Split([string[]] "01-",[System.StringSplitOptions]"None")[1]

In the above PowerShell script, the $sysdate variable stores the date. To split the string of the first occurrence of a substring, we used the PowerShell string Split() method.

The Split () method takes substring 01- and breaks the string into multiple substrings. It split the string on the first occurrence of 01- substring and returns the remaining part as a substring.

The output of the above PowerShell script to split a string on the first occurrence of substring returns 08-2022 as a substring.

PS C:\> $sysdate = "01-08-2022"                                                                                         

PS C:\> $sysdate.Split([string[]] "01-",[System.StringSplitOptions]"None")[1]                                           
08-2022

Cool Tip: How to split string to get the last element in PowerShell!

Split Operator to Split String on First Occurrence of Character in PowerShell

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

$sysdate = "01-08-2022" 

# Split a string on first occurrences of character
$day,$month_year = $sysdate -split '\-' ,2  

$day 
$month_year

In the above PowerShell script, the $sysdate variable stores the date. To split the string on the first occurrence of a character Hyphen (-), we used the split operator.

The output of the above PowerShell script to split a string by Hyphen delimiter for the first occurrence returns 08-2022 as a substring.

PS C:\> $sysdate = "01-08-2022"                                                                                         

PS C:\> $day,$month_year = $sysdate -split '\-' ,2                                                                      

PS C:\> $day                                                                                                            
01

PS C:\> $month_year                                                                                                     
08-2022

PS C:\> 

Conclusion

I hope the above article on how to split a string on the first occurrence of substring or character in PowerShell is helpful to you.

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