PowerShell script to find the users folder size and last modified then csv export

PowerShell script to find the users folder size and last modified more than 90 days and csv export
January 23, 2023

PowerShell script to find the users folder size and last modified then csv export

This script will loop through all the folders in the “C:\Users” directory and check if they are a container (i.e. a folder). If they are, it will calculate the size of the folder (including all subfolders and files) using the Measure-Object cmdlet and the last modified time of the folder using the Sort-Object and Select-Object cmdlets. It will then add the user name, folder size, and last modified time to a results array. After all the users have been processed, it will export the results array to a CSV file named “user_folder_sizes.csv” on the desktop of the user running the script.

You can change the path of the export-csv command to any other location where you want to save the file.

$users = Get-ChildItem -Path “C:\Users”
$results = @()

foreach ($user in $users) {
if ($user.PSIsContainer) {
$folderSize = (Get-ChildItem $user.FullName -Recurse | Measure-Object -Property Length -Sum).Sum
$folderLastModified = (Get-ChildItem $user.FullName -Recurse | Sort-Object LastWriteTime -Descending | Select-Object -First 1).LastWriteTime
$results += New-Object PSObject -Property @{
‘User’ = $user.Name
‘Size (MB)’ = [math]::Round(($folderSize/1MB),2)
‘Last Modified’ = $folderLastModified
}
}
}

$results | Export-Csv -Path “C:\Users\user\Desktop\user_folder_sizes.csv” -NoTypeInformation

 

Leave a Reply

Your email address will not be published. Required fields are marked *