Powershell

How to use PowerShell Split-Path

PowerShell Split-Path cmdlet helps to retrieve the specific part of the path. It is possible to divide a long path address into subpaths, e.g., parent folders, subfolders, and files. The Split-Path cmdlet of PowerShell filters the Path according to the properties supported by each path. This writeup provides a deep insight into the working and usage of the Split-Path cmdlet of PowerShell.

How PowerShell Split-Path works

The PowerShell Split-Path cmdlet works on the following syntax:

> Split-Path -Path <path> <parameters>

The parametric support of the Split-Path cmdlet allows the users to retrieve a specific part of that path.

  •  Like the Leaf parameter, it prints the end node of a path:
  • The LeafBase parameter returns the name(base) of the path only(excludes the extension part).
  • The Parent parameter can be used to get the parent node of a path:
  • The Qualifier and NoQualifier options provide the facility to retrieve and exclude the root drive of that path respectively.
  • Extension parameter shows the ending part (extension) of the leaf node in a path.

Generally, the Split-Path cmdlet accepts the paths and returns the parent folder. However, its use can be extended by using parameters. The parameters defined above are practiced in the examples of the upcoming section.

How to use PowerShell Split-Path

As described earlier, PowerShell Split-Path can split the provided path into possible categories that may be parent, child, or their leaves. We have provided a deep insight into the Split-Path cmdlet by exercising a few examples and the parameters supported by it.

Example 1: The following PowerShell command practices the use of the Split-Path cmdlet on a path provided in the command. It is observed that the command does not contain any parameters, therefore the output would represent the parent path node only (i.e. E:\linuxhint).

> Split-Path -Path 'E:\linuxhint\authors.txt'

Apart from using the recommended syntax of Split-Path, it can also be piped with any address. For instance, the command provided below pipes the Split-Path cmdlet with the same path.

> 'E:\linuxhint\authors.txt' | Split-Path

The –Parent parameter of the Split-Path cmdlet also behaves the same as the above commands.

> Split-Path -Path 'E:\linuxhint\authors.txt' -Parent

Note: It is recommended to enclose the path in quotation marks to avoid errors as the quotes are necessary if the path contains space.

Example 2: Using -Leaf
The leaf refers to getting to the end node of a path. The command provided below retrieves the complete Leaf part of the Path:

> Split-Path -Path 'E:\linuxhint\authors.txt' -Leaf

The Leaf command also allows you to get the content inside a directory. For instance, the command written below will show the content that resides in the parent path(E:\linuxhint).

Note: The -Resolve option allows you to get the content in the presence of the wildcard character. Without using -Resolve, you won’t be able to execute the command using the (*) wildcard character.

> Split-Path -Path 'E:\linuxhint\*' -Leaf -Resolve

Example 3: Using Qualifier/No Qualifier
The qualifier option returns the root directory of the Path. For instance, if the path “E:\linuxhint\authors.txt” is examined using the qualifier parameter, the result would be E: and it can be seen in the output shown below.

> Split-Path -Path 'E:\linuxhint\authors.txt' -Qualifier

If you want to get a path other than the root node of the path the NoQualifer parameter of the Split-Path is used. The command written below practices the NoQualifier parameter on a path “E:\linuxhint\authors.txt“.

> Split-Path -Path 'E:\linuxhint\authors.txt' -NoQualifier

Example 4:Using IsAbsolute
The IsAbsolute parameter returns a Boolean value (True or False) and an absolute path returns True value whereas it is false if the path is not absolute. For instance, the command provided below returns True as the path provided by following the proper syntax of a path:

> Split-Path -Path 'E:\linuxhint\authors.txt' -IsAbsolute

The result of IsAbsolute is false in the following case as we have removed the Qualifier from the Path.

> Split-Path -Path '\linuxhint\authors.txt' -IsAbsolute

Example 5: Using Extension and LeafBase
The Extension parameter prints the extension of a leaf node as per your input. If you add the extension in the path, then it will display that, otherwise, if you don’t mention it in the path, then it will show a void(blank). The following command returns .txt as we have used it in the past.

> Split-Path -Path 'E:\linuxhint\authors.txt' -Extension

Now, executing the same command without mentioning the extension in the path will print a blank line as can be seen in the image below:

> Split-Path -Path 'E:\linuxhint\authors' -Extension

The LeafBase operator acts oppositely to the Extension parameter. LeafBase shows the name of the leaf node of a path. For instance, the command written below extracts the name(first part) of the leaf node:

> Split-Path -Path 'E:\linuxhint\authors.docx' -LeafBase

Conclusion

The PowerShell Split-Path cmdlet helps in getting the desired part of any path. The possible parts of the path can also be retrieved using the Split-Path cmdlet in PowerShell. This article practices the implementation of PowerShell Split-Path. You have learned a sequential retrieval of paths by following the examples provided in this post. Each example refers to the use of the Split-Path cmdlet and gets a specific part of the path.

About the author

Adnan Shabbir