none
Retrieve the size of user profile using powershell. RRS feed

  • Question

  • Hi,

    I am creating a powershell script which should return exact size of the user profile.

    below is a cmdlet I am using

    Get-ChildItem C:\Users\$profile -force -recurse | Measure-Object -Sum length | Select -ExpandProperty Sum

    however after running powershell as an administrator also i'm getting "Access Denied" Error for many items.

    is there any way I can resolve this error.


    • Moved by Bill_Stewart Friday, March 15, 2019 7:07 PM This is not "teach me system admin basics" forum
    Wednesday, February 6, 2019 11:23 AM

All replies

  • Remove the -force option. It is causing get-childitem to return junctions like "application data" which point to other directories within appdata. 
    Wednesday, February 6, 2019 2:12 PM
  • Hi,

    Couple of things:

    - You don't want to use the $profile variable as it's an automatic variable, it returns the path to the current user, current host profile:

    C:\Users\<username>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

    So to prevent issues or unwanted behavior, I'd recommend using a different variable name or use the $env:USERNAME variable, which is probably what you want to use here.

    - As Moto pointed out, the -force switch will cause it to try and search directories beyond the actual profile.

    - You may also want to use the -erroraction SilentlyContinue switch as well, as the Get-ChildItem cmdlet can behave strangely with certain directories/files

    All in all, the command should look like this:

    get-childitem C:\Users\$env:USERNAME -Recurse -ErrorAction SilentlyContinue | Measure-Object -Sum Length | Select-Object -ExpandProperty Sum


    Please remember to mark the post(s), which answered your question.

    Wednesday, February 6, 2019 2:34 PM
  • User profiles are inaccessible even to an admin without taking ownership or adding the admin account to the profile.  This is by design with modern Windows.

    Also the "Measure-Object won't tell you the profiles size correctly as it is being used.

    To correctly get user profiles location use the WMI class that is designed for this purpose.

    Get-WmiObject win32_userprofile -Filter 'Special=False' | Select-Object LocalPath


    \_(ツ)_/

    Wednesday, February 6, 2019 3:52 PM