Home » PowerShell » PowerShell Split Path into Array

PowerShell Split Path into Array

To split the path into an array in PowerShell, use PowerShell String Split() function or split operator. The directory path is separated by character backslash (\), using the backslash as a delimiter, which breaks the directory path into a string array.

In this article, we will discuss using the PowerShell string Split() function and split operator to split the path into an array.

Use Split() to Split Path into Array in PowerShell

Use the PowerShell String built-in Split() function to split the path into an array.

Refer to the below steps to split the path into an array in PowerShell

  1. Declare a string in PowerShell and store it in a variable.
  2. Call the PowerShell Split() string method over a variable.
  3. Use the backslash (\) character as a delimiter to break the string into an array.
$path = "C:\Windows\System32\calc.exe" 

# Use the backslash as delimiter to splits the string into array
$path.Split("\") 

In the above PowerShell script, the PowerShell string Split() function uses backslash as a delimiter to split the path into a string array.

The output of the above script to get the path into an array is:

PS C:\> $path = "C:\Windows\System32\calc.exe"  

PS C:\> $path.Split("\")                                                                                                

C:
Windows
System32
calc.exe
PS C:\>  

Use Split operator to Split Directory Path into Array

You can use PowerShell split operator to split a directory path into an array. Use the backslash (\) character in the split operator for breaking the path into a string array.

Refer to the below steps to split the path into an array using the split operator in PowerShell

  1. Declare a string in PowerShell and store it in a variable.
  2. Use PowerShell split operator over variable
  3. Use the backslash (\\) character as a delimiter to break the path into a string array
$dirpath = "D:\PS\Alias.txt" 

# use the split operator and pass \\ as delimiter
$dirpath -split '\\' 

In the PowerShell script, the split operator uses a backslash character to split the path into an array.

The output of the above PowerShell script after breaking the path into string array is:

PS C:\> $dirpath = "D:\PS\Alias.txt"                                                                                    

PS C:\> $dirpath -split '\\'                                                                                            
D:
PS
Alias.txt

PS C:\>                                                                                                                                

Cool Tip: How to use GetEnumerator in PowerShell to read array data!

Conclusion

I hope the above article on how to use PowerShell string Split() method and split operator to split the path into an array is helpful to you.

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