How To Split Path Into Array In PowerShell?

PowerShell provides the Split-Path Cmdlet to split path into an array. In this PowerShell tutorial, I will explain how to split a path into an array in PowerShell using different methods with examples.

To split a file path into an array in PowerShell, use the Split-Path cmdlet in PowerShell. For example, to get the parent folder of a path, use $parentPath = Split-Path -Path ‘C:\Folder\File.txt’ -Parent. To get the file name without the directory, use $fileName = Split-Path -Path ‘C:\Folder\File.txt’ -Leaf. And to get just the extension, use $extension = Split-Path -Path ‘C:\Folder\File.txt’ -Extension. Each of these commands will give you different parts of the path as individual strings, not as an array.

Split Path Into Array In PowerShell

In PowerShell, a path is a string that specifies the location of a file, folder, or registry key in your file system. Paths can be absolute or relative, and they usually contain a series of directory names separated by backslashes on Windows (\).

Splitting a path into an array can be useful for several reasons:

  • To manipulate individual parts of the path.
  • To dynamically create new paths based on user input or other variables.
  • To validate paths or check the existence of certain directories within a path.

Now, let us check various methods to split path into array in PowerShell.

Method 1: Using the Split-Path Cmdlet

PowerShell provides a built-in cmdlet called Split-Path that allows you to split the path into different elements. Here is a complete example.

# Example of using Split-Path to get the parent directory
$fullPath = 'C:\Users\Public\Documents\Report.xlsx'
$parentPath = Split-Path -Path $fullPath -Parent
Write-Output $parentPath  # Outputs: C:\Users\Public\Documents

However, Split-Path does not directly split a path into an array of all its components. It’s used to get specific parts of a path, like the parent directory or leaf element.

Once you execute the PowerShell script, you can see the output in the screenshot below. Note: I have used the VS code editor here.

Split Path Into Array In PowerShell

Method 2: Using the -split Operator

To split a file path into an array in PowerShell, use the -split operator with the path separator. For example, $pathArray = $path -split '\\' will split a Windows path into an array at each backslash. Alternatively, you can use the Split-Path cmdlet for more granular control over the parts of the path you want to extract.

To split the entire path into an array, you can use the -split operator with the correct delimiter.

# Example of splitting a path into an array
$fullPath = 'C:\Users\Public\Documents\Report.xlsx'
$pathArray = $fullPath -split '\\'
Write-Output $pathArray  # Outputs: C:, Users, Public, Documents, Report.xlsx

In this example, we’re using \\ as the delimiter because the backslash is an escape character in PowerShell and must be escaped.

You can see the output in the screenshot below.

How To Split Path Into Array In PowerShell

Method 3: Using the .Split() Method

Another way to split a path into an array in PowerShell is by using the .Split() method of a string object.

Here is the complete script for splitting path to an array in PowerShell.

# Example of using the .Split() method
$fullPath = 'C:\Users\Public\Documents\Report.xlsx'
$pathArray = $fullPath.Split('\')
Write-Output $pathArray  # Outputs: C:, Users, Public, Documents, Report.xlsx

Remember that you might need to remove empty entries if your path starts with a backslash, as the first element of the array will be empty.

Method 4: Using the Convert-Path and -split Combination

If you’re dealing with PowerShell paths that may include PowerShell providers (like FileSystem::), you may first want to convert the path into a standard file system path.

# Example of converting and splitting a path
$providerPath = 'FileSystem::C:\Users\Public\Documents\Report.xlsx'
$fullPath = Convert-Path $providerPath
$pathArray = $fullPath -split '\\'
Write-Output $pathArray  # Outputs: C:, Users, Public, Documents, Report.xlsx

When splitting paths, you may encounter paths with spaces or other special characters. Enclose such paths in quotes to ensure they are treated as single strings.

Conclusion

Splitting a path into an array in PowerShell is a very common requirement and can be achieved using several methods. Whether you choose the Split-Path cmdlet for specific elements, the -split operator, or the .Split() method for full control, PowerShell provides the flexibility to work with paths effectively.

I hope now you are confident to use any of the above methods to split a path to an array in PowerShell.

You may also like: