none
Show size of folders using powershell RRS feed

  • Question

  • I have a task I need to do, which is get a list of user profiles and show how large the profile folders are in size, preferably megabytes or what ever. I am a bit rusty on getting this info and hoping someone can help with the right direction. I have the following so far, not much but yeah..

    $directory = Get-Item -Path "C:\users"

    $directory | Get-ChildItem

    So this gives me all the profiles on the machine, and if I do like a $directory[0].fullname I can bring up the full path to the profile.  But that is all I found, I know I done this before but need refresh

    I'm wanting to do a foreach, and list the following in each:

    Profile fullname, Total objects in profile, Size of profile, in a table format

    If anyone knows of any good articles I can read and follow, or if you got any tips to help me further that would be appreciated.

    Thank You



    JCtech1123, Cheers

    Friday, October 7, 2016 7:53 PM

Answers

  • I would recommend using the module NTFSSecurity for this. It provides the cmdlet Enable-Privileges that enables the Backup privilege. Thanks to that you can really read all the files even if you do not have access to it. Of course you can use the cmdlet only as an administrator.Also this module works around the max path limitation of 260 characters if you use the dir2 cmdlet instead of the built in dir cmdlet.

    Enable-Privileges
    dir2 d:\ -Recurse | Measure-Object -Property Length -Sum


    -Raimund

    Monday, October 10, 2016 2:14 PM

All replies

  • Friday, October 7, 2016 7:55 PM
  • Have you checked the repository? (As you are no doubt aware, this forum is for scripting questions rather than script requests).


    -- Bill Stewart [Bill_Stewart]

    Friday, October 7, 2016 7:58 PM
    Moderator
  • Measure-Object -Property Length -Sum should get you the file count, Sum (size) as well. 
    Saturday, October 8, 2016 12:25 PM
  • I would recommend using the module NTFSSecurity for this. It provides the cmdlet Enable-Privileges that enables the Backup privilege. Thanks to that you can really read all the files even if you do not have access to it. Of course you can use the cmdlet only as an administrator.Also this module works around the max path limitation of 260 characters if you use the dir2 cmdlet instead of the built in dir cmdlet.

    Enable-Privileges
    dir2 d:\ -Recurse | Measure-Object -Property Length -Sum


    -Raimund

    Monday, October 10, 2016 2:14 PM
  • Reply too all above, the issue I am seeing is that when my loop checks the other profiles like another user, its throwing the following:

    Measure-Object : The property "Length" cannot be found in the input for any objects.

    If I run the following:

    Get-ChildItem -Path "C:\Users\profilename" -Recurse | Measure-Object -Property *

    I see that Property "Length" does not return.  Not sure if this is expected.


    JCtech1123, Cheers

    Monday, October 10, 2016 11:02 PM
  • Do you have files in the folder you are looking at? Folders do not have a length which is

    -Raimund

    Tuesday, October 11, 2016 7:11 AM
  • Get-ChildItem C:\Users -Directory | Foreach {
        $Files = Get-ChildItem $_.FullName -Recurse -File
        $Size = '{0:N2}' -f (( $Files | Measure-Object -Property Length -Sum).Sum /1MB)
        [PSCustomObject]@{Profile = $_.FullName ; TotalObjects = "$($Files.Count)" ; SizeMB = $Size}
    }

    Tuesday, October 11, 2016 2:25 PM
  • As this is about profiles, I would not recommend using Get-ChildItem as you might not have the required permissions to read all the profiles. Plus long paths could make some folders unreachable.

    -Raimund

    Tuesday, October 11, 2016 6:57 PM
  • Ok so we have gotten farther with this script.  Turns out that after -Recurse switch, we ended up adding -force, which I guess forced powershell to look at even hidden files.  so now I am able to pull the list of more than 100 or so profile folders and display their size.  My issues now is I'm wanting to sort the folders by size from biggest to smallest.  Some profiles are like 0.00 mb.  Others are over 3.5 gb.  When I try to use the sort-object -descending against my $array, it doesn't work.  How would I target the folder size property and sort by that?  This will be really helpful in my team pinning down large profiles.

    Thoughts appreciated,

    Thank You


    JCtech1123, Cheers

    Friday, October 21, 2016 11:11 PM
  • For custom scripting jobs, you will need to write it yourself or get a tool such as WinDirStat to help you.

    Unfortunately we don't have the resources for writing custom code on request.


    -- Bill Stewart [Bill_Stewart]

    Monday, October 24, 2016 2:34 PM
    Moderator
  • Thanks for the input everyone, turns out this is going to have to be a custom script for sure.

    JCtech1123, Cheers

    Wednesday, October 26, 2016 12:08 AM