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' -Parent
Split-Path -path 'C:\Windows\System32\drivers\etc\hosts' -Parent
Split-Path -path 'C:\Windows\System32\drivers\etc\hosts' -Parent
Split-Path -path 'C:\Windows\System32'
Split-Path -path 'C:\Windows\System32' -Parent
# Several paths can be returned
Split-Path -path 'C:\Windows\System32\notepad.exe','C:\Windows\System32'
# Keep only the drive name
Split-Path -path 'C:\Windows\System32' -Qualifier
Split-Path -path HKCU:\Software -Qualifier
# Keep only the path without the drive name
Split-Path -path 'C:\Windows\System32' -NoQualifier
Split-Path -path HKCU:\Software -NoQualifier
# Display the name of the element
Split-Path -path 'C:\Windows\System32\drivers\etc\hosts' -Leaf
Split-Path -path 'C:\Windows\System32' -Leaf
Split-Path -path HKCU:\Software -Leaf
# Check if this is an absolute path
Split-Path -path 'C:\Windows\System32\drivers\etc\hosts' -IsAbsolute
Split-Path -path . -IsAbsolute
# Check if this path exists
Split-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 path
5..15 | Set-Content {'file{0}.txt' -f $_ } -WhatIf
# Without the -resolve the result is inconclusive
Split-Path -path .\fichier*.txt -Leaf
Split-Path -path .\fichier*.txt -Leaf -Resolve
Split-Path -path .\fichier1?.txt -Leaf -Resolve
Split-Path -path C:\[wu]* -Leaf -Resolve
# Powershell 6 minimum
# Show extension only
Split-Path -path 'C:\Windows\System32\notepad.exe' -Extension
# Show only the name without the extension
Split-Path -path 'C:\Windows\System32\notepad.exe' -LeafBase

Related links