CSV 文件中的用户配置文件名称和大小

User Profile Names and Sizes into CSV File

此脚本获取计算机上的用户配置文件,我能够将输出提供到文本文件中,但我不知道如何将此信息导出到 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"
}

您需要创建一个具有属性的对象,然后使用 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