如何修改PowerShell脚本,为网络目录CSV清单添加文件/文件夹类型列
修改后的PowerShell脚本
$Directory ="X:\Sub\General" Get-ChildItem -Path $Directory -Recurse -Force | ForEach {[PSCustomObject]@{ FullPath = $_.FullName FileName = $_.Name Size = $_.Length Extension = $_.Extension Creation = $_.CreationTime LastWriteTime = $_.LastWriteTime ItemType = if ($_.PSIsContainer) { "文件夹" } else { "文件" } } } | Export-Csv -Path "C:\users\abc\Downloads\fulllist.csv" -NoTypeInformation
关键修改说明
- 新增
ItemType列,利用PowerShell内置的PSIsContainer属性判断条目类型:- 若
$_.PSIsContainer为$true,则标记为文件夹 - 若为
$false,则标记为文件
- 若
- 给
Export-Csv添加-NoTypeInformation参数,避免CSV文件开头生成多余的类型注释行,让输出格式更简洁规范。
内容的提问来源于stack exchange,提问作者Dinesh Padmanabhan
相关产品推荐
相关产品推荐

