Powershell

How to Split Paths With the PowerShell Split-Path Cmdlet

The “Split-Path” cmdlet is used in PowerShell to split a specific path. This operation is performed to get the specific parts of the path address. Moreover, paths can be split into several individual parts, such as Qualifier, NoQualifier, Parent, or Leaf. The “Split-Path” cmdlet filters the path according to the provided option, like “-Parent” or “-Leaf”. Splitting file paths also helps users understand path addresses easily.

This article will discuss a way to fix the mentioned query.

How to Split Paths Using the PowerShell Split-Path Cmdlet?

These methods can be utilized to split a path:

Method 1: Use “Split-Path” Cmdlet to Split “-Qualifier”

The qualifier is the drive letter part of the address which could be “C:”, “D:” or any other drive letter. More specifically, the “-Qualifier” parameter will be added at the end of the path to split it.

Example

This example will illustrate a method to split a qualifier using the “Split-Path” cmdlet:

Split-Path C:\Doc\File.txt -Qualifier

According to the above code:

  • First, add the “Split-Path” cmdlet.
  • After that add the path that needs to be split.
  • Finally, specify the parameter “-Qualifier” to split the qualifier:

The qualifier path has been split successfully.

Method 2: Use “Split-Path” Cmdlet to Split “-NoQualifier”

The “-NoQualifier” parameter is used to split the part of the address other than the drive letter. The parameter “-NoQualifier” is added at the end of the command.

Example

Now, we will split the path that is not a part of the qualifier:

Split-Path C:\Doc\File.txt -NoQualifier

Method 3: Use “Split-Path” Cmdlet to Split “-Parent”

The “-Parent” parameter is used to split the address except for the file name and extension. It only displays the directory address but does not split the file name and extension.

Example

In this demonstration, the parent part of the path will be split using the “Split-Path” cmdlet and the “-Parent” option:

Split-Path C:\Doc\File.txt -Parent

Method 4: Use “Split-Path” Cmdlet to Split “-Leaf”

The “-Leaf” parameter is used to split out the filename and extension. When this parameter is utilized at the end of the address then it only displays the filename and extension.

Example

Now, let’s split the leaf part of the path address:

Split-Path C:\Doc\File.txt -Leaf

That was all about splitting paths in PowerShell.

Conclusion

To split paths in PowerShell, first, add the “Split-Path” cmdlet. After that, add the address that needs to be split. Finally, specify the parameters such as “Qualifier”, “-NoQualifier”, “-Parent”, and “-Leaf” to split the paths, as per requirements. This blog has presented a guide to split a path using the “Split-Path” cmdlet.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.