How to Split Paths with the PowerShell Split


The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

Split is a PowerShell command to divide one or more files into multiple output paths. It uses the .NET Framework’s IEnumerable interface and is similar in function to cut-and-paste, but it takes its input from standard input instead of a file. Split can be used for splitting text files at word boundaries, as well as any other delimiter that you choose.

The “powershell split-path into array” is a PowerShell cmdlet that allows users to split the path of a file or directory.

How to Split Paths with the PowerShell Split

Is it necessary to break pathways in order to get certain pieces of it? If that’s the case, PowerShell Split-Path is your best buddy. You’ll see that paths reflect the locations of files, certificates, and the registry while working with them.

You may also need to filter which parts of a route to return at some point, say when automating. Fortunately, this post will show you how to do so using the PowerShell Split-Path cmdlet.

Let’s get this party started! Continue reading to learn how to add a new weapon to your PowerShell armory!


You’ll need a computer running any contemporary version of Windows with PowerShell 5.1 or above to follow along with the exercises in this hands-on lesson. This article will use PowerShell 7.1.1 and Windows 10 20H2.

What Is PowerShell and Why Should I Use It?

What’s the Difference Between a Path and a Road?

Before you get started with the PowerShell Split-Path cmdlet, make sure you understand what a path is. The position of an item that follows a specified format is determined by a route.

A file’s path may be C:demosubfolder1TestFile 11.txt, for example. If you divide this journey into sections, you’ll find the following:

  • The drive, qualification, or defined route is designated by the letter C. The segment of the route from the left up to the colon (:) character is known as the qualifier.
  • The folder demosubfolder1 and its subfolder(s) or containers.
  • The filename or leaf is TestFile 11.txt. A path’s last piece or component is the leaf.

But keep in mind that paths aren’t only for files and directories. Other viable options include:

  • a certification (e.g., Cert:CurrentUserMy)
  • the registrar (e.g., HKCU:Software)
  • Purpose (e.g., Function:New-Guid)
  • Adaptable (e.g., Variable:PSEdition)
  • AD (for example, AD:CN=Users,DC=Constoso,DC=com)

Get-ChildItem: Listing Files, Registry, and Certificates is related to Get-ChildItem: Listing Files, Registry, and Certificates.

Split-Path Parameters in PowerShell

Split-Path, like every cmdlet, has a collection of arguments that control how the cmdlet operates and produces its result. The PowerShell Split-Path syntax is made up of these arguments.

  • -Path – You may use this argument to divide one or more path strings. Pipeline input is also accepted for this parameter. Paths using wildcards are likewise permitted.
  • -LiteralPath — The -LiteralPath argument takes paths to divide, much as the -Path parameter. However, this argument will only work with the route as specified. If you use this argument to provide paths, the program will not parse wildcards.
  • -Resolve – This option instructs the Split-Path cmdlet to resolve any files or items referenced by the path you specified. This argument cannot be used in conjunction with the -LiteralPath option due to its literal character.
  • -IsAbsolute — This argument returns a boolean value that indicates whether or not the route you supplied is absolute.

The -Credential argument of the PowerShell Split-Path cmdlet reportedly supports a credential PSCredential object. However, according to Microsoft, none of the out-of-the-box PowerShell providers accept this argument, and using it would result in an error.

The Split Location Parameters are the following parameters. The PowerShell Split-Route cmdlet uses these arguments to determine which piece or portion of a path to return. You can only use one of these options at a time since the cmdlet can only return one element of a route.

  • -Parent – Returns the parent location of the path you gave (without the item or filename). Because this option is also the default split location argument, you may leave it blank and still retrieve the parent location.
  • -Leaf – Only returns the path’s final element, or the leaf.
  • -LeafBase – Only returns the path’s last element or the leaf without the extension. Only PowerShell 6.0 and above support this argument.
  • -Extension -Returns merely the extension of the leaf (from the final dot “.” to the path’s last character). Only PowerShell 6.0 and above support this argument.
  • -Qualifier – Only returns the path’s drive or qualifier.
  • -NoQualifier – The drive or qualifier is removed from the remainder of the route.

Using the Split-Path cmdlet in PowerShell (Examples)

You may separate and dissect sections of a route using the PowerShell Split-Path cmdlet. You may then select which section of a route to return after that. You may opt to return the qualifier, the path without a qualifier, or the filename, depending on your output requirements (s).

Getting a Path’s Parent Folder

Run the PowerShell Split-Path cmdlet with the -Parent option to get the path’s parent folder.

Split-Path -Parent C:demosubfolder1TestFile 11.txt -Path C:demosubfolder1TestFile 11.txt

The command returns the parent folder’s path, as seen in the output below.

Returning the Item's parent containers Returning the Item’s parent containers

Tip: If you use the PowerShell Split-Path cmdlet without any arguments, the parent folder will be returned by default, which is the same as if you use the -Parent option.

Tip: Because the -Path argument takes multiple paths, you may divide several path string values in a single command.

Split-Path -Path, for example. ‘c:folder1′,’c:folder2’

Without the Qualifier, a Path is Displayed

Consider the following scenario: you’re writing a script that copies a folder tree structure from one disk to another (e.g., C:demosubfolder1 to D:demosubfolder1). You may wish to separate the source path and just retrieve the folder structure without the drive letter in your code.

Run the PowerShell Split-Path cmdlet with the -NoQualifier option to receive the path without the qualifier, as seen below. This command separates the path while leaving the drive letter out of the output.

-NoQualifier Split-Path -Path C:demosubfolder1

The noQualifier option returns path but without the qualifier string, as seen in the example below.

Returning to the Qualifier-Free Path Returning to the Qualifier-Free Path

Obtaining a Path’s Drive or Qualifier

It’s possible that you’ll simply need to return the qualifier or the drive letter at times. When you want your script to summarize the data depending on drive letters, for instance.

To accomplish so, use the -Qualifier argument in the PowerShell Split-Path command below. As a consequence of this command, the path will be divided and just the qualification string will be returned.

Split-Path -Qualifier C:demosubfolder1TestFile 11.txt -Path C:demosubfolder1TestFile 11.txt

Returning the Path's Drive or Qualifier Returning the Drive or Qualifier of the Path

The Name of a File, Directory, or Item is Displayed

Consider a road to be a tree. The qualifier may represents the tree, folders represent the branches, and the leaf represents the finish. Any non-zero length string at the path’s end is referred to as a leaf.

Run the Divide-Route command with the -Leaf argument when you need to split a path to retrieve the leaf.

-Path C:demosubfolder1TestFile 11.txt -Leaf Split-Path -Path C:demosubfolder1TestFile 11.txt

As a consequence, the command only returned the filename from the path you gave, as seen in the figure below.

Returning the Path's Leaf Returning the Leaf from the Path

PowerShell 6.0: Splitting the Filename and Extension

This information only relates to PowerShell 6.0 and later versions.

You’ve separated the route and returned the object, correct? In this situation, the item is a filename (TestFile 11.txt), which is made up of two parts: the base and the extension. With the -LeafBase and -Extension parameters of the PowerShell Separate-Path cmdlet, you may further split the leaf into these two pieces.

Run the commands below to find out what a filename’s base and extension are.

The Extension option will return an empty string if the route does not have an extension.

-Path C:demosubfolder1TestFile 11.txt -Leaf Split-Path -Path C:demosubfolder1TestFile 11.txtBase Split-Path -Path C:demosubfolder1TestFile_11.txt -Extension

Splitting the Leaf's Base and Extension Splitting the Leaf’s Extension and Base

Filename and Extension Splitting (Windows PowerShell 5.1)

Unfortunately, with Windows PowerShell 5.1 and earlier versions, the -LeafBase and -Extension arguments are not accessible. Don’t worry, even if you just have Windows PowerShell 5.1, you can reproduce the output of these parameters with some PowerShell magic.

Instead of using the Split-Path cmdlet, you’ll use the next best thing: the split() technique combined with the -replace operator.

Related: [Examples] Using PowerShell Replace to Replace Text

PowerShell Strings: Concatenate, Expand, Format, and Everything

A split() function exists in a PowerShell string object, such as the path. This technique divides a string into numerous components using a delimiter character that you provide. In the case of paths, the back-slash character () is used as a delimiter.

Understanding PowerShell Objects (Back to Basics)

The -replace operator, on the other hand, allows you to replace strings using regular expressions (RegEx).

Getting Started with PowerShell and Regex is a good place to start.

Run the PowerShell command below to get the same results as the -LeafBase argument.

# Split the path (‘C:demosubfolder1TestFile_11.txt’) using ” as the delimiter. # Select the last ‘[-1]’ element after the split (TestFile_11.txt) # Search the string that matches this pattern –> ‘.[^.]*$’ # ^ this pattern will match the last dot “.” in the path AND every other character after it. # Then replace the match with nothing/empty value ”. (‘C:demosubfolder1TestFile_11.txt’).split(”)[-1] -replace ‘.[^.]*$’,”

As a consequence, the command simply produced the filename’s basic name, as seen in the picture below.

Using PowerShell 5.1 to get the file base Using PowerShell 5.1 to get the file base

Run the command below in PowerShell to retrieve simply the file extension.

# Use the separator ‘.’ to split the path (‘C:demosubfolder1TestFile 11.txt’). # After the split (txt), choose the final ‘[-1]’ element (‘C:demosubfolder1TestFile 11.txt’). split(‘.’) [-1]

The command simply returned the filename extension – txt, as seen in the output below.

5.1 Obtaining the file extension in PowerShell 5.1 Obtaining the file extension in PowerShell

Identifying whether or not the path is absolute

You’ll come across two sorts of paths as a system administrator: absolute and relative. What’s the difference, though? A qualifier, such as C:demo or HKCU:Software, precedes an absolute path. A relative path, on the other hand, does not contain a qualifier, such as.demo or folder1folder2.

The -IsAbsolute argument of the PowerShell Split-Path cmdlet may be used to determine an absolute path. To do so, use the commands listed below to check whether the path is absolute.

Split-Path -Path C:demosubfolder1TestFile 11.txt -IsAbsolute # This path is absolute Split-Path -Path.demosubfolder1TestFile 11.txt -IsAbsolute # This path is relative Split-Path -Path.demosubfolder1TestFile 11.txt -IsAbsolute

The -isAbsolute argument, as seen below, produces a boolean result indicating whether the route is absolute (TRUE) or relative (FALSE) (FALSE).

Identifying whether or not the path is absolute Identifying whether or not the path is absolute

Using Wildcards to Split and Resolve Paths

The routes you separate using the Split-Path cmdlet don’t have to be existent at this stage. This cmdlet will divide a path whether it exists or not and return the result.

However, the Split-Path cmdlet in PowerShell adds a new argument called -Resolve. You may use this option to resolve objects that are referred by wildcards. If you use this argument, the route you want to divide as well as the objects inside it must already exist.

For example, execute the command below to get the objects that match the *.txt file extension. The -Leaf argument guarantees that just the items, not the parent containers, are returned by the cmdlet.

The (*) wildcard denotes one or more characters to match, while the (?) denotes a single character.

-Path C:demosubfolder1*.txt -Leaf -Resolve Split-Path -Path C:demosubfolder1*.txt -Leaf -Resolve

The files TestFile 11.txt through TestFile 20.txt are listed in the output below. The filename extension for all of these files was.txt.

Resolving filenames that are similar Resolving filenames that are similar

Replace the asterisk (*) wildcard with the (?) wildcard, like in the command below, to resolve a single character match.

-Path C:demosubfolder1TestFile 2?.txt -Resolve -Leaf Split-Path -Path C:demosubfolder1TestFile 2?.txt

Because this was the only file that matched, the report below only displays the file TestFile 20.txt.

Identifying and resolving single character matches Identifying and resolving single character matches

To resolve the route, wrap it in single quote marks if it includes an escape character, such as the grave-accent (“‘). The command below, for example, separates and resolves a route that contains an escape character.

‘C:demosubfolder1dir’nTestFile 2?.txt’ Split-Path -Path ‘C:demosubfolder1dir’nTestFile 2?.txt’ -Resolve -Leaf

Using Escape Characters to Split and Resolve a Path Using Escape Characters to Split and Resolve a Path

If you do not contain the path in single quotation marks, the command will produce an error stating that the route does not exist as a proof of concept. If you use double quote marks instead, you’ll get the same issue.

Split-Path -Path C:demosubfolder1dir’nTestFile 2?.txt -Leaf -Resolve Split-Path -Path C:demosubfolder1dir’nTestFile 2?.txt -Leaf -Resolve Split-Path -Path “C:demosubfolder1dir’nTestFile 2?.txt” -Leaf -Resolve Split-Path -Path “C:demosubfolder1dir’nTestFile 2?.txt” -Leaf -Resolve

Without enclosing single quote marks, resolving a Path Without enclosing single quote marks, resolving a Path


The Split-Path cmdlet in PowerShell is a must-have utility for both system administrators and users. The goal of this article was to show you how to use the Divide-Route cmdlet to split any path and return certain components depending on whatever argument you use.

Have you ever utilized Split-Path in a task? If not, has this lesson persuaded you to include it into your manual or automated tasks? Or do you know of a technique to separate routes that is more efficient than PowerShellSplit-Path?

The “powershell resolve-path” is a PowerShell cmdlet that can split paths into their components. The “powershell resolve-path” will be able to split paths that have spaces in them.

Frequently Asked Questions

How do I split a path in PowerShell?

A: To split a path in PowerShell, you must use the Path.Split function and pass it two parameters for the separator (the character to separate) and how many times you want that segment of your string separated -in this case 2.

How does split work in PowerShell?

A: When you press the space bar, it opens up a split. There is also a Cmd key if thats your thing.

How do you split a variable in PowerShell?

A: Please refer to this article for more information on splitting variables in PowerShell.

Related Tags

  • split-path examples
  • powershell split-path filename without extension
  • powershell split path environment variable
  • powershell split-path last folder
  • split-path : a parameter cannot be found that matches parameter name ‘leafbase’.

Table of Content