Powershell - Split-Path pour couper un chemin

Pour me soutenir, vous pouvez vous abonner à la chaîne, partager et liker les vidéos, désactiver votre bloqueur de pub ou encore faire un don. Merci!

La commande Split-path permet de découper un chemin issu d’un fournisseur.
L’existance du chemin n’est pas vérifiée par défaut, pour cela utilisez l’argument -resolve ou Test-Path
Pour rappel -LiteralPath ne prend pas en compte les caractères génériques * ? [abc]…

# Afficher le parent, argument -parent (valeur par défaut)
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'
Split-Path -path 'C:\Windows\System32\drivers\etc\hosts' -Parent
Split-Path -path 'C:\Windows\System32'
Split-Path -path 'C:\Windows\System32' -Parent
# Plusieurs chemins peuvent être renvoyés
Split-Path -path 'C:\Windows\System32\notepad.exe','C:\Windows\System32'
# Conserver uniquement le nom du lecteur
Split-Path -path 'C:\Windows\System32' -Qualifier
Split-Path -path HKCU:\Software -Qualifier
# Conserver uniquement le chemin sans le nom de lecteur
Split-Path -path 'C:\Windows\System32' -NoQualifier
Split-Path -path HKCU:\Software -NoQualifier
# Afficher le nom de l'élément
Split-Path -path 'C:\Windows\System32\drivers\etc\hosts' -Leaf
Split-Path -path 'C:\Windows\System32' -Leaf
Split-Path -path HKCU:\Software -Leaf
# Vérifier si c'est un chemin absolu
Split-Path -path 'C:\Windows\System32\drivers\etc\hosts' -IsAbsolute
Split-Path -path . -IsAbsolute
# Vérifier si c'est le chemin existe
Split-Path -path 'C:\Windows\System32\drivers\etc\hosts' -Resolve -Leaf
# Il est possible de vérifer l'existance de plusieurs éléments
# Création de fichier dans le chemin courant
5..15 | Set-Content {'fichier{0}.txt' -f $_ } -WhatIf
# Sans le -resolve le résultat n'est pas concluant
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
# Afficher uniquement l'extension
Split-Path -path 'C:\Windows\System32\notepad.exe' -Extension
# Afficher uniquement le nom sans l'extension
Split-Path -path 'C:\Windows\System32\notepad.exe' -LeafBase

Liens en relation