Vérifier si un fichier existe dans Windows PowerShell
-
Utilisez
Test-Path
pour vérifier si un fichier existe dans PowerShell -
Utilisez
[System.IO.File]::Exists()
pour vérifier si un fichier existe dans PowerShell -
Utilisez
Get-Item
pour vérifier si un fichier existe dans PowerShell -
Utilisez
Get-ChildItem
pour vérifier si un fichier existe dans PowerShell

Parfois, vous obtenez un message d’erreur indiquant que le fichier n’existe pas dans PowerShell. Ce tutoriel présentera quatre méthodes pour vérifier si un fichier existe dans PowerShell.
Utilisez Test-Path
pour vérifier si un fichier existe dans PowerShell
La première méthode est le cmdlet Test-Path
. Il détermine si le chemin complet existe. Elle renvoie $True
si le chemin existe et $False
s’il manque un élément. Le paramètre -PathType Leaf
vérifie un fichier et non un répertoire.
Test-Path -Path "C:/New/test.txt" -PathType Leaf
Production :
True
S’il n’y a pas de fichier nommé file.txt
dans le répertoire New
, il renvoie $False
.
Test-Path -Path "C:/New/file.txt" -PathType Leaf
Production :
False
Utilisez [System.IO.File]::Exists()
pour vérifier si un fichier existe dans PowerShell
Une autre méthode pour vérifier si un fichier existe est [System.IO.File]::Exists()
. Il fournit un résultat booléen, True
si le fichier existe ou False
si le fichier n’existe pas.
[System.IO.File]::Exists("C:/New/test.txt")
Production :
True
Utilisez Get-Item
pour vérifier si un fichier existe dans PowerShell
la cmdlet Get-Item
est utilisée pour obtenir l’élément au chemin spécifié. Vous pouvez l’utiliser pour vérifier si un fichier existe en spécifiant le chemin du fichier. Il imprime le mode (attributs), l’heure de la dernière écriture, la longueur et le nom d’un fichier s’il existe. Il affiche un message d’erreur si un fichier n’existe pas.
Get-Item C:/New/test.txt
Production :
Directory: C:\New
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12/11/2021 2:59 PM 5 test.txt
Vous trouverez ci-dessous la sortie lorsque le fichier n’existe pas.
Get-Item : Cannot find path 'C:\New\test10.txt' because it does not exist.
At line:1 char:1
+ Get-Item C:/test/test10.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\New\test10.txt:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Utilisez Get-ChildItem
pour vérifier si un fichier existe dans PowerShell
La dernière méthode consiste à utiliser la cmdlet Get-ChildItem
. Elle récupère les éléments et les éléments enfants dans un ou plusieurs chemins spécifiés. Elle affiche les détails du fichier si celui-ci existe dans le chemin spécifié.
Get-ChildItem -Path C:/New/test.txt
Production :
Directory: C:\New
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12/11/2021 2:59 PM 5 test.txt
Il imprime un message d’erreur disant Cannot find path '$path' because it does not exist.
lorsqu’un fichier n’est pas trouvé.
Get-ChildItem -Path C:/New/test
Production :
Get-ChildItem : Cannot find path 'C:\New\test' because it does not exist.
At line:1 char:1
+ Get-ChildItem -Path C:/New/test
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\New\test:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Article connexe - PowerShell File
- Comment créer un fichier texte en utilisant Windows PowerShell
- Comment débloquer des fichiers en utilisant PowerShell
- Comment obtenir la taille d'un fichier en Ko en utilisant PowerShell
- Comment obtenir la version du fichier dans PowerShell
- Comment stocker le contenu d'un fichier texte dans une variable en utilisant PowerShell
- Comment télécharger des fichiers avec FTP dans PowerShell