Forum Discussion

Baron164's avatar
Baron164
Brass Contributor
Sep 13, 2022

Need help pulling profile folder sizes from workstations

I am in the process of rolling out folder redirection for our users. We currently have over 1000 workstations and I need to get an idea of the size of users profiles so I can size the file server accordingly.  I am attempting to write a script that will connect to each workstation, check each user profile folder, and record the size of the folders. 

Here is what I have so far.

 

Remove-Item \\server\share\Workstations.csv

$Workstations = Get-ADComputer -filter * -SearchBase "OU=Workstations,OU=Test,DC=Domain,DC=com" -Properties * | select -Property DNSHostName

foreach($pc in $Workstations)
{
    $hostname = $pc.DNSHostName

    foreach($user in Get-ChildItem \\$hostname\C$\Users -Directory | Where-Object {$_.Directory -ne 'Administrator','Public','Default'})
    {
        $appdata = Get-ChildItem \\$hostname\C$\Users\$user\appdata\Roaming -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        $contacts = Get-ChildItem \\$hostname\C$\Users\$user\contacts -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        $desktop = Get-ChildItem \\$hostname\C$\Users\$user\Desktop -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        $documents = Get-ChildItem \\$hostname\C$\Users\$user\Documents -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        $downloads = Get-ChildItem \\$hostname\C$\Users\$user\downloads -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        $favorites = Get-ChildItem \\$hostname\C$\Users\$user\favorites -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        $links = Get-ChildItem \\$hostname\C$\Users\$user\links -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        $music = Get-ChildItem \\$hostname\C$\Users\$user\music -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        $pictures = Get-ChildItem \\$hostname\C$\Users\$user\pictures -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        $startmenu = Get-ChildItem \\$hostname\C$\Users\$user\startmenu -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        $videos = Get-ChildItem \\$hostname\C$\Users\$user\videos -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select -ExpandProperty Sum
        
        $object = New-Object -TypeName PSObject
        $object | Add-Member -MemberType NoteProperty -Name Hostname -Value $hostname
        $object | Add-Member -MemberType NoteProperty -Name User -Value $user
        $object | Add-Member -MemberType NoteProperty -Name AppData -Value $appdata
        $object | Add-Member -MemberType NoteProperty -Name Contacts -Value $contacts
        $object | Add-Member -MemberType NoteProperty -Name Desktop -Value $desktop
        $object | Add-Member -MemberType NoteProperty -Name Documents -Value $documents
        $object | Add-Member -MemberType NoteProperty -Name Downloads -Value $downloads
        $object | Add-Member -MemberType NoteProperty -Name Favorites -Value $favorites
        $object | Add-Member -MemberType NoteProperty -Name Links -Value $links
        $object | Add-Member -MemberType NoteProperty -Name Music -Value $music
        $object | Add-Member -MemberType NoteProperty -Name Pictures -Value $pictures
        $object | Add-Member -MemberType NoteProperty -Name StartMenu -Value $startmenu
        $object | Add-Member -MemberType NoteProperty -Name Videos -Value $videos

        $object | export-csv \\server\share\Workstations.csv -NoTypeInformation -Append   
    }
}

 

Here is what the CSV looks like as of right now.

"Hostname","User","AppData","Contacts","Desktop","Documents","Downloads","Favorites","Links","Music","Pictures","StartMenu","Videos"
"WS01.Domain.com","Administrator","69491033","412","282","241396","282","690","1985","504","884",,"504"


The script hangs at this point and won't go any further. I would also prefer to exclude profiles like the Administrator profile but the exclude I included in the script does not appear to be working either. 

I am not sure how best to go about doing this and any help would be greatly appreciated.

  • Perhaps it's a permissions thing, if you connect to a computer using UNC, can you actually open the folder? (Not exclusive rights for just the user?)

    Another approach would be to create a Scheduled Task on each computer which starts a report script after the user logs in (5 minutes after login for example) and outputs the report in a share as username_timestamp. The user has rights on their profile and the scan will have no issue I guess?
    • Baron164's avatar
      Baron164
      Brass Contributor
      I'll look into the permission angel. I've considered that running it locally and doing a file per user. But with 1000+ workstations, that would be a lot of reports to dig through. Having them all append a single file would be nice, but at that point I'm basically looking at building a database.
      • You can always to type *.txt >> output.txt to combine all the files 😉 I hope it works out for you 🙂

Resources