PowerShell Split-Path Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Test-Path cmdlet in PowerShell.
https://cloudaffaire.com/powershell-test-path-cmdlet/
In this blog post, we will discuss Split-Path cmdlet in PowerShell. You can use Split-Path cmdlet to split a path of an item into segments and get the required segment. For example, you Split-Path cmdlet can be used to get a file or directory name from a path. Split-Path cmdlet can also be used to test if a given path is absolute or relative. Split-Path cmdlet can be used in the registry as well.
Split-Path Cmdlet Syntax:
1 2 3 4 5 6 7 8 |
## Split-Path ## [-Path] ## [-Parent] | [-NoQualifier] | [-Leaf] | [-Qualifier] ## [-Resolve] # [-IsAbsolute] ## [-Credential ## [-UseTransaction] ## [ |
Split-Path Cmdlet Argument List:
- –Credential: To impersonate another user or elevate your credentials.
- –IsAbsolute: Indicates that this cmdlet returns $True if the path is absolute and $False if it is relative.
- –Leaf: Indicates that this cmdlet returns only the last item or container in the path.
- –LiteralPath: Specifies the paths to be split. Unlike Path, the value of LiteralPath is used exactly as it is typed.
- –NoQualifier: Indicates that this cmdlet returns the path without the qualifier.
- –Parent: Indicates that this cmdlet returns only the parent containers of the item or of the container specified by the path.
- –Path: Specifies the paths to be split. Wildcard characters are permitted. If the path includes spaces, enclose it in quotation marks.
- –Qualifier: Indicates that this cmdlet returns only the qualifier of the specified path.
- –Resolve: Indicates that this cmdlet displays the items that are referenced by the resulting split path instead of displaying the path elements.
- –UseTransaction: Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
PowerShell Split-Path Cmdlet:
Get Only File Or Directory Name From A Path In PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
###################################### ## PowerShell | Cmdlet | Split-Path ## ###################################### ## PowerShell Latest Version (5) ## create some items for this demo New-Item -ItemType Directory -Path "C:\Dir1" New-Item -ItemType File -Path "C:\Dir1\File1.txt" New-Item -Path HKCU:\Software -Name MyRegKey -Value "CloudAffaire" ## get only file or directory name from a path in PowerShell Split-Path -Path "C:\Dir1\File1.txt" -Leaf |
Get The Parent Directory Path Of A File Or Directory In PowerShell:
1 2 3 |
## get parent directory path of a file or directory in PowerShell Split-Path -Path "C:\Dir1\File1.txt" -Parent |
Get The Drive Letter From A File Or Directory Path In PowerShell:
1 2 3 |
## get drive name from a path of a directory or file in PowerShell Split-Path -Path "C:\Dir1\File1.txt" -Qualifier |
Get The Relative Path Of A File Or Directory In PowerShell:
1 2 3 |
## get relative path from an absolute path of a file or directory in PowerShell Split-Path -Path "C:\Dir1\File1.txt" -NoQualifier |
Check If A Path Of A File Or Directory Is Valid In PowerShell:
1 2 3 4 |
## check if a path of a file or directory is valid in PowerShell Split-Path -Path "C:\Dir1\File1.txt" -Resolve Split-Path -Path "C:\Dir1\File2.txt" -Resolve |
Check If A Path Of A File Or Directory Is Absolute Path In PowerShell:
1 2 3 4 |
## check if a given path is absolute or relative in PowerShell Split-Path -Path "C:\Dir1\File1.txt" -IsAbsolute Split-Path -Path "Dir1\File1.txt" -IsAbsolute |
Get Only Registry Key Name From A Path In PowerShell:
1 2 3 |
## get only registry key name from a registry path in PowerShell Split-Path -Path "HKCU:\Software\MyRegKey" -Leaf |
Get The Parent Location Of A Registry Key In PowerShell:
1 2 3 |
## get parent path of a registry key in PowerShell Split-Path -Path "HKCU:\Software\MyRegKey" -Parent |
Get Root Registry Location Of A Registry Key In PowerShell:
1 2 3 |
## get root location of a registry key in PowerShell Split-Path -Path "HKCU:\Software\MyRegKey" -Qualifier |
Get Relative Path Of A Registry Key In PowerShell:
1 2 3 |
## get a relative path for a registry key Split-Path -Path "HKCU:\Software\MyRegKey" -NoQualifier |
Check If A Given Registry Key Path Is Valid In PowerShell:
1 2 3 4 |
## check if a given registry key path is valid Split-Path -Path "HKCU:\Software\MyRegKey" -Resolve Split-Path -Path "HKCU:\Software\MyRegKey1" -Resolve |
You Can Also Use Split-Path Cmdlet To Split Path Of A FQDN, though it is not meant to be used with s FQDN.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## you can also use split-path cmdlet to split an FQDN $path = "https://cloudaffaire.com/category/powershell/" Split-Path -Path $path -Leaf Split-Path -Path $path -Parent Split-Path -Path $path -Qualifier Split-Path -Path $path -NoQualifier Split-Path -Path $path -IsAbsolute ## remove the items created for this demo Remove-Item -Path C:\Dir1 -Recurse -Force Remove-Item -Path "HKCU:\Software\MyRegKey" |
Hope you have enjoyed this article. In the next blog post, we will discuss Join-Path cmdlet in PowerShell.
To Set more details on PowerShell, kindly follow below official documentation