Muni Bus

パソコンの操作方法や設定方法を忘れないようにメモしています。ブログを一回引っ越ししているので、所々表示がかなり乱れています・・・

【PowerShell】ファイルやディレクトリーの存在を確認する

Test-Pathコマンドレットを使う。存在すればTrue、存在しなければFalseを返す。-PathTypeオプションにLeaf(ファイル)かContainer(ディレクトリー)を指定することで、ファイルかディレクトリーの指定をすることができる。特に指定をしなければ、ファイルかディレクトリーが存在すればTrueを返す。

> Test-Path C:\Windows\explorer.exe
True
> Test-Path C:\Windows
True
> Test-Path C:\Windows\explorer.exe -PathType Leaf
True
> Test-Path C:\Windows\explorer.exe -PathType Container
False
> Test-Path C:\Windows -PathType Leaf
False
> Test-Path C:\Windows -PathType Container
True
> Test-Path C:\Windows\*.exe -PathType Leaf
True
> Test-Path C:\Windows\*.exeexe -PathType Leaf
False