
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use Split-Path Command in PowerShell
Split-Path is to retrieve the part of the specified path, such as a parent folder, a subfolder, or a file name. It can also tell if the path is relative or absolute.
This command supports a few parameters which help to retrieve the part of the specified path. Consider we have below executable file path and we will see how the Split-Path command will retrieve the parent folder and subfolder as well as the root directory.
'C:\Temp\PsExec.exe'
Default Split-Path command will retrieve the parent folder name of the file.
PS C:\> Split-Path 'C:\Temp\PsExec.exe' C:\Temp
Here the default parameter is -Parent, which retrieves the parent folder path. Above command is similar to,
PS C:\> Split-Path 'C:\Temp\PsExec.exe' -Parent C:\Temp
If you need only file name then use the -Leaf parameter.
PS C:\> Split-Path 'C:\Temp\PsExec.exe' -Leaf PsExec.exe
To retrieve the root directory, you need to use the -Qualifier parameter.
PS C:\> Split-Path 'C:\Temp\PsExec.exe' -Qualifier C:
If you want to check if the path is absolute or relative then use the -IsAbsolute Parameter.
PS C:\> Split-Path 'C:\Temp\PsExec.exe' -IsAbsolute True PS C:\Temp> Split-Path .\PsExec.exe -IsAbsolute False
Now, let say you want the file names inside the folder so if we write below command that won’t give the desired output.
Output
PS C:\> Split-Path C:\Scripts\* -Leaf *
The output is * only and the command didn’t retrieve the files/folders name. To solve this issue, -Resolve parameter is used.
Split-Path C:\scripts\* -Leaf -Resolve
Output
DSCTest Lab01 MyModules VS_Installation DscTest.ps1 Servers.txt
You can see the files and folder names. Similarly, if you want to retrieve the specific extension files, you can use both the parameters -Leaf and -Resolve.
PS C:\> Split-Path C:\scripts\*.ps1 -Leaf -Resolve DscTest.ps1
Similarly, you can apply Split-Path command for the registry.
Split-Path 'HKCU:\Software\Microsoft\Internet Explorer\Control Panel\' HKCU:\Software\Microsoft\Internet Explorer Split-Path 'HKCU:\Software\Microsoft\Internet Explorer\Control Panel\' -Qualifier HKCU: Split-Path 'HKCU:\Software\Microsoft\Internet Explorer\Control Panel\' -Leaf Control Panel