Search code examples
powershellhome-directory

powershell command to list all users, their home directories + sizes


I need a script or command that would list all users on a computer plus their home directories and sizes of their home directories. I can do it only for users are looged in. I also managed to get a list of all users :

$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
$adsi.Children | where {$_.SchemaClassName -eq 'user'} | Select-Object @{n='UserName'
e={$_.Name}}

but I don't know how to get home dir, plus size from that list.

thanks in advance


Solution

  • Here is a powershell loop that outputs objects with the properties you are looking for (Name, LocalPath, FolderSize), formatted as a table. This combines a few techniques to get the values you are looking for.

    Get-WmiObject win32_userprofile | % { 
        try {
            $out = new-object psobject
            $out | Add-Member noteproperty Name (New-Object System.Security.Principal.SecurityIdentifier($_.SID)).Translate([System.Security.Principal.NTAccount]).Value
            $out | Add-Member noteproperty LocalPath $_.LocalPath
            $out | Add-Member noteproperty FolderSize ("{0:N2}" -f ((Get-ChildItem -Recurse $_.LocalPath | Measure-Object -property length -sum -ErrorAction SilentlyContinue).sum / 1MB) + " MB")
            $out
        } catch {}
    } | Format-Table