Skip to content

How to track user profile size with PowerShell

Jordan Hammond fun headshot
Jordan Hammond|April 22, 2020
image (53)
image (53)

PDQ PowerShell Scanner is here, and the answer to the question “Can I pull this data” is most likely yes! Instead of just telling you it is awesome, we figured it would be best to show you some ideas. We have these up in GitHub; I can’t talk about how much I like these enough. Hopefully, they will give you some ideas on other information you can gather. If it does, please submit a pull request following the guidelines. We would love to build this out to something epic. In this blog, we will go over tracking user profile size.

User Profile Size

This one is a quick script. It uses Get-Childitem and Measure-Object commands to get your results. First, grab all directories in the “C:\Users” directory.

$UserFolders = Get-ChildItem -Path "C:\Users" -Force -Directory

Each of those goes into a foreach loop where you run a recursive Get-Childitem that you pipe into Measure-Object. We are forcing the data we get from this to be an integer because that format makes reports built to show up correctly.

[UInt64]$FolderSize = ( Get-Childitem -Path $Folder.FullName -Force -Recurse | Measure-Object -Property "Length" -Sum ).Sum

And finally, you put your results into a pscustom object so you can format your output into a readable format. This will ensure that the results that get put into Inventory look exactly how you would want.

Putting It all Together

Let’s take a look at the complete script. We highly recommend that if you are going to use it you get it from GIT and follow the steps in there so your scanner will remain up to date if changes are made.

$ErrorActionPreference = "SilentlyContinue" $UserFolders = Get-ChildItem -Path "C:\Users" -Force -Directory ForEach ( $Folder in $UserFolders ) { [UInt64]$FolderSize = ( Get-Childitem -Path $Folder.FullName -Force -Recurse | Measure-Object -Property "Length" -Sum ).Sum [PSCustomObject]@{ FolderName = $Folder.BaseName FolderPath = $Folder.FullName Size = $FolderSize } }

Conclusion

This is a short script, and it highlights that it does not take much to pull in some real value. Hopefully, this will get your creative juices going and help you get started on building your perfect scanner. If you have been wondering how you are going to track specific data, well, now you know. Get started already!

Jordan Hammond fun headshot
Jordan Hammond

Jordan had spent his life wondering why tasks he didn’t like to do had no options to complete themselves. Eventually he had to make that happen on his own. It turned out that he enjoyed making tasks complete themselves, and PDQ thought that is something he should talk about on the internet.

Related articles