Muni Bus

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

【PowerShell】あるディレクトリ内のファイル一覧を得る

Get-ChildItemコマンドレットの結果をパイプラインでWhere-Objectコマンドレットに渡してフィルターをかける。PsIsContainerプロパティはファイルであればFalse、ディレクトリであればTrueを返す。

PS > Get-ChildItem C:\Windows\Boot
    ディレクトリ: C:\Windows\Boot
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2021/06/05     21:10                DVD
d-----        2023/04/12     22:17                EFI
d-----        2021/06/05     21:10                Fonts
d-----        2021/06/05     21:10                Misc
d-----        2023/04/12     22:17                PCAT
d-----        2021/06/23     17:21                Resources
-a----        2021/06/05     21:05             91 BootDebuggerFiles.ini
PS > Get-ChildItem C:\Windows\Boot | Where-Object {!$_.PsIsContainer}
    ディレクトリ: C:\Windows\Boot
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2021/06/05     21:05             91 BootDebuggerFiles.ini

Where-Objectコマンドレットは?というエイリアスが定義されている。

PS > Get-ChildItem C:\Windows\Boot | ? {!$_.PsIsContainer}
    ディレクトリ: C:\Windows\Boot
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2021/06/05     21:05             91 BootDebuggerFiles.ini