Muni Bus

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

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

Test-Pathコマンドレットを使う。オプションを何も指定しなければ、いずれかが存在すればTrueを返す。ファイルかディレクトリどちらかに限定したい場合は、-typeオプションにLeafを指定しればファイル、Containerを指定すればディレクトリが対象になる。

PS > Test-Path C:\Windows\win.ini
True
PS > Test-Path C:\Windows
True
PS > Test-Path C:\Windows\win.inii
False
PS > Test-Path C:\Windowss
False
PS > Test-Path -type Leaf C:\Windows\win.ini
True
PS > Test-Path -type Leaf C:\Windows
False
PS > Test-Path -type Container C:\Windows\win.ini
False
PS > Test-Path -type Container C:\Windows
True