How To Use PowerShell Split-Path Cmdlet | 7 Best Examples

A parent folder, a subdirectory, or a file name are some examples of the parts of the supplied path that Split-Path is intended to obtain. It can also determine whether the path is absolute or relative. Several parameters that aid to get the specified portion of the path are supported by this command.

How To Use PowerShell Split-Path Cmdlet | 7 Best Examples

What’s in a PowerShell Path?

An item that adheres to a particular format has a path that identifies its placement. The path to a file might be, for instance, C:\folder\subfolder1\File.txt. If you divide this journey into its component components, the following is on the path:

The drive, the qualifier, or the designated path is C:. The portion of the route leading from the left up to the colon (:) character is known as the qualifier.
The folder and its associated subfolders are designated by the name \folder\subfolder1.
The filename, or leaf, is file.txt. The final component or piece of a path is the leaf.

But keep in mind that paths are not just for files and directories. Other legitimate directions could be:

  • Certificate (e.g., Cert:\CurrentUser\My)
  • Function (e.g., Function:\New-Guid)
  • Active Directory (e.g., AD:\CN=Users,DC=Constoso,DC=com)
  • Registry (e.g., HKCU:\Software)
  • Variable (e.g., Variable:\PSEdition)

What are the PowerShell Split-Path Parameters?

Split-Path -path has a collection of parameters, just like any other cmdlet, that may be used to change how it operates and what output it produces. The PowerShell Split-Path syntax is made up of these parameters.

  1. -Path: You can split one or more path strings using this option. Pipeline input is also accepted for this parameter. Path wildcards are also permitted.
  2. -LiteralPath: The -LiteralPath option accepts paths to divide, just like the -Path parameter does. The path will only be handled by this parameter exactly as it is written. Meaning that if you use this option to define paths, the command won’t understand wildcards.
  3. -Resolve: The -Resolve argument instructs the Split-Path cmdlet to resolve any files or objects that the path you specified references. This argument cannot be used in conjunction with the -LiteralPath option because of its literal character.
  4. -IsAbsolute: The -IsAbsolute argument determines whether or not the path is absolute path by returning a boolean value.

The Split Location Parameters are the ones listed below. These options specify the component or portion of a path the PowerShell Split-Path cmdlet should return. You may only use one of these options at a time because the cmdlet can only return one element from a path. 

  1. -Parent: The -Parent returns the parent folder of the specified path you gave (without the item or filename). You can skip this parameter and still retrieve the parent location because it is also the default split location parameter.
  2.  -Leaf:  The -leaf only returns the final component of the path or leaf.
  3. -LeafBase: Only the last element of the path or the leaf without the extension is returned by the -LeafBase function. PowerShell 6.0 and later is the only version that supports this option.
  4. -Extension: Only the leaf’s extension (from the final dot “.” to the final character of the route) is returned by the extension option.
  5. -Qualifier: The -Qualifier only returns the path’s drive or qualifier.
  6. -NoQualifier: Removes the disk or qualifier from the rest of the path with the command “-NoQualifier.”

How Can PowerShell Sleep Help in Split-Path Cmdlet?

The startsleep cmdlet in PowerShell can aid in utilizing the Split-Path cmdlet effectively. By using startsleep, you can introduce a delay in the script, allowing time for previous commands to complete. This ensures that Split-Path executes correctly, avoiding any potential errors or inconsistencies in the output.

Method to Use PowerShell split-path Cmdlet (With Examples)

Using the PowerShell Split-Path cmdlet, you may divide and analyze a path into its component pieces. You can then define the section of a path to return. You can opt to return the qualifier, the path without a qualifier, or the filename(s) depending on your output requirements.

1. Displaying Path Without the Qualifier

Make a script that copies a folder tree structure from one drive to another if you need to. You might want to divide the source path in your code so that it only receives the folder structure and not the drive letter.

Run the PowerShell Split-Path cmdlet with the -NoQualifier parameter, as seen in the command below, to obtain the path without the qualifier. With this command, the path is divided while the drive letter is not included in the output.

Split-Path -Path C:\folder\subfolder1 -NoQualifier
Displaying Path Without the Qualifier

2. Displaying The Parent Folder Of The Path

Use the PowerShell Split-Path cmdlet and the -Parent option to retrieve the path’s parent folder.

Split-Path -Path C:\folder\subfolder1\File.txt -Parent
Displaying The Parent Folder Of The Path

3. Displaying The Drive or Qualifier of a Path

Use the -Qualifier argument to run the PowerShell Split-Path command listed below. The route will be split by this command, and as a result, it will only return the qualification string.

Split-Path -Path C:\folder\subfolder1\File.txt -Qualifier
Displaying The Drive or Qualifier of a Path

4. Displaying the Name of a File or Leaf

Run the Split-Path command below and add the -Leaf parameter whenever you need to split a path to acquire the leaf (last file).

Split-Path -Path C:\folder\subfolder1\File.txt -Leaf
Displaying the Name of a File or Leaf

The command only returned the filename from the path that you gave as a result.

5. Splitting the Filename and Extension (PowerShell 6.0)

With the -LeafBase and -Extension parameters of the PowerShell Split-Path cmdlet, you can divide the leaf into these two halves.

Run the commands listed below to obtain a filename’s base and extension. The Extension option will yield an empty string if the path has no extensions.

Split-Path -Path C:\folder\subfolder1\File.txt -LeafBase
Split-Path -Path C:\folder\subfolder1\File.txt -Extension

6. Splitting and Resolving Paths with Wildcards

Another parameter for the Split-Path cmdlet in PowerShell is called -Resolve. You can resolve the objects that wildcard references refer to using this argument. Additionally, the path you will split and the things inside must already exist if you use this argument.

Run the command below, for instance, to return the items matching the *.txt file extension. The cmdlet returns only the items, not the parent containers, thanks to the -Leaf argument.

While (?) is a single-character wildcard, (*) denotes one or more characters to match.

Split-Path -Path D:\abc\*.txt -Leaf -Resolve
Splitting and Resolving Paths with Wildcards

The output listed includes all of the files whose filename extensions matched.txt.

Instead, use the (?) wildcard in place of the asterisk (*) wildcard to resolve a single-character match.

Split-Path -Path D:\abc\1234?.txt -Resolve -Leaf
image 174

Because this file was the only one that matched, the report below simply displays the file 1234.txt.

Put the path inside single quotation marks if it contains an escape character, such as the grave accent (“‘), in order to resolve the path. 
The command will return an error saying that the path does not exist if you do not enclose that path in single quotation marks. If you choose to use double quotation marks instead, the identical mistake still occurs.

7. Determining if the Path is Absolute

A qualifier, such as C:folder or HKCU:Software, precedes an absolute path. A relative path, on the other hand, lacks qualifiers like.folder or.foldersubfolder.

Using the -IsAbsolute argument of the PowerShell Split-Path cmdlet, you may determine whether a path is absolute. Run the commands to check if the path is absolute to do this.

Split-Path -Path D:\abc\1234.txt -IsAbsolute
Determining if the Path is Absolute true
Split-Path -Path .abc\1234.txt -IsAbsolute
Determining if the Path is Absolute false

If the path is absolute (TRUE) or relative (FALSE), the -isAbsolute option returns a boolean value.