1

该脚本获取机器上的用户配置文件,我能够将输出提供到文本文件中,但我无法弄清楚如何将此信息导出到 CSV 文件。

dir C:\Users | foreach -Begin {} -Process {
    $size = (dir $_.FullName -Recurse -Force -EA SilentlyContinue | Measure-Object ‘length’ -Sum -Maximum).Sum
    Write-Output ("{0:n2}" -f ($size/1MB) + " MB", $_.FullName) >> "C:\scripts\UserProfiles\UserProfiles.txt"
}
4

1 回答 1

4

您需要创建一个具有属性的对象,然后使用Export-Csv

Get-ChildItem C:\Users |
    ForEach-Object `
        -Begin { Write-Host -Object "Scanning user directories" } `
        -Process {
            Write-Host "Scanning path '$($_.FullName)'"
            $Size = (Get-ChildItem $_.FullName -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object ‘length’ -Sum -Maximum).Sum

            [pscustomobject] @{
                Name = $_.Name
                Path = $_.FullName
                Size = '{0:N2} MB' -f ( $Size / 1MB )
            }
        } |
        Export-Csv -Path C:\Scripts\UserProfiles\UserProfiles.csv -NoTypeInformation
于 2018-10-19T14:04:50.380 回答