Powershell - Split-Path to cut a path
To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!
The Split-path command is used to split a path from a provider.
The existence of the path is not checked by default, to do this use the -resolve or Test-Path
argument. As a reminder -LiteralPath does not take into account wildcards *? [abc]…
# Show parent, argument -parent (default)Split-Path -path 'C:\Windows\System32\notepad.exe'Split-Path -path 'C:\Windows\System32\notepad.exe' -ParentSplit-Path -path 'C:\Windows\System32\drivers\etc\hosts' -ParentSplit-Path -path 'C:\Windows\System32\drivers\etc\hosts' -ParentSplit-Path -path 'C:\Windows\System32'Split-Path -path 'C:\Windows\System32' -Parent# Several paths can be returnedSplit-Path -path 'C:\Windows\System32\notepad.exe','C:\Windows\System32'
# Keep only the drive nameSplit-Path -path 'C:\Windows\System32' -QualifierSplit-Path -path HKCU:\Software -Qualifier
# Keep only the path without the drive nameSplit-Path -path 'C:\Windows\System32' -NoQualifierSplit-Path -path HKCU:\Software -NoQualifier
# Display the name of the elementSplit-Path -path 'C:\Windows\System32\drivers\etc\hosts' -LeafSplit-Path -path 'C:\Windows\System32' -LeafSplit-Path -path HKCU:\Software -Leaf
# Check if this is an absolute pathSplit-Path -path 'C:\Windows\System32\drivers\etc\hosts' -IsAbsoluteSplit-Path -path . -IsAbsolute
# Check if this path existsSplit-Path -path 'C:\Windows\System32drivers\etc\hosts' -Resolve -Leaf
# It is possible to check the existence of several elements# Creation of a file in the current path5..15 | Set-Content {'file{0}.txt' -f $_ } -WhatIf# Without the -resolve the result is inconclusiveSplit-Path -path .\fichier*.txt -LeafSplit-Path -path .\fichier*.txt -Leaf -ResolveSplit-Path -path .\fichier1?.txt -Leaf -ResolveSplit-Path -path C:\[wu]* -Leaf -Resolve
# Powershell 6 minimum# Show extension onlySplit-Path -path 'C:\Windows\System32\notepad.exe' -Extension# Show only the name without the extensionSplit-Path -path 'C:\Windows\System32\notepad.exe' -LeafBase
Related links
Powershell - Testing network connectivity and port accessibility
Testing network connectivity and port accessibility with PowershellPowershell - Display network connections (equivalent to netstat)
Display network connections (listening ports, active connections...)Powershell - Testing name resolution (equivalent to nslookup)
Powershell commands to test name resolution (equivalent to nslookup)Powershell - View and manage DNS configuration of network interfaces
Powershell commands to display and manage DNS configuration of network interfacesPowershell - Managing IP configuration of network interfaces
Powershell commands to view and modify the IP configuration of network interfacesPowershell - Managing the status and configuration of network interfaces
Powershell commands to view and modify the status and configuration of network interfaces (disable IPv6, enable/disable an interface)
Follow me on
Support me
Last content
Powershell - Testing network connectivity and port accessibility
Powershell - Display network connections (equivalent to netstat)
Powershell - Testing name resolution (equivalent to nslookup)
Powershell - View and manage DNS configuration of network interfaces
Powershell - Managing IP configuration of network interfaces
Powershell - Managing the status and configuration of network interfaces
Powershell and the Left Hand Side
Powershell - Managing disks, partitions and volumes