如何使用 PowerShell 中的 Get-ChildItem 命令仅获取文件而不获取文件夹?
powershellmicrosoft technologiessoftware & coding
要仅获取文件,您需要在 Directory 属性参数中使用 NOT 运算符。
示例
Get-ChildItem D:\Temp\ -Attributes !Directory
输出
irectory: D:\Temp Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 07-05-2018 23:00 301 cars.xml -a---- 29-12-2017 15:16 4526 healthcheck.htm -a---- 29-12-2017 15:16 4526 healthcheck1.htm -ar--- 13-01-2020 18:19 0 Readonlyfile.txt -a---- 08-12-2017 10:24 48362 servicereport.htm -a---- 08-12-2017 10:24 48362 servicereport1.htm -a---- 08-12-2017 10:16 393 style.css -a---- 12-12-2017 23:04 1034 tes.htmoutput.htm -a---- 08-12-2017 11:29 7974 Test.xlsx -a---- 25-10-2017 08:13 104 testcsv.csv
您可以组合使用不同的选项来获得所需的结果。例如,仅获取隐藏文件,而不获取文件夹。
Get-ChildItem D:\Temp\ -Attributes !Directory -Hidden
仅获取系统只读文件,
Get-ChildItem D:\Temp\ -Attributes !Directory –System -Readonly

