我有以下脚本:
Get-ChildItem $rootPath -Recurse |
Where-Object {$_.PSIsContainer} |
Where-Object {(Get-ChildItem $_.FullName | Where-Object {!$_.PSIsContainer}| Measure-Object | Select-Object -ExpandProperty Count) -gt 0} |
ForEach-Object{
$files = Get-ChildItem $_.FullName
$props = @{
Path = $_.FullName
Size = "{0:N0}" -f (($files | Where-Object {!$_.PSIsContainer} | Measure-Object -Sum Length | Select-Object -ExpandProperty Sum))
Count = $files | Measure-Object | Select-Object -ExpandProperty Count
}
If ($files.Extension -match "L\d\d"){
# These are special files and we are assuming they are alone in the directory
# Change the path
$props.Path = $files | Select-Object -First 1 | Select-Object -ExpandProperty FullName
}
New-Object -TypeName PSCustomObject -Property $props
} | Select Path,Size,Count如何将输出写入文本文件而不是控制台?
发布于 2015-07-17 12:54:59
将其转发到Out-File。例如:
Get-ChildItem ...
<skipped>
} | Select Path,Size,Count | Out-File "files.txt"发布于 2015-07-17 15:52:16
几件事。你在这里还有其他的选择值得一提。
} | Select Path,Size,Count | Set-Content "files.txt"Set-Content也可以工作,并具有默认ACSII编码的优点。如果性能发挥作用,那么使用.Net StreamWriter无论如何都会击败Set-Content和Out-File。
更重要的是,此代码旨在输出对象。使用它时,您应该使用Export-CSV来具有良好的格式化输出。
} | Select Path,Size,Count | Export-Csv -NoTypeInformation "files.csv"https://stackoverflow.com/questions/31476280
复制相似问题