Powershell : Space Report for User Profile.

In the world is weird requirements  I had a need to get a list of users with how much space their profile had used in a colour coded format so I knew how much data I would be copying to a new volume, this was to check the space being used did not cause an issue with the volume drive.

All you need to do is defined the text file or users, the name of the share you are looking for and then the output file for the report.

Colour Coding the Results.

Green for sizes up to 500MB
Yellow for sizes between 500MB and 2.5GB
Red for sizes over 2.5GB.

The Script

# Path to the text file containing the list of usernames
$userListPath = "userlist.txt"

# Path to the user home directories
$basePath = "\\bear-smb.bear.local\users$"

# Output file path
$outputPath = "sizereport.txt"

# Function to get folder size
function Get-FolderSize {
    param (
        [string]$folderPath
    )

# Check if the directory exists
   if (Test-Path -Path $folderPath -PathType Container) {

# Calculate the size of the directory

        $size = (Get-ChildItem -Path $folderPath -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
        return $size
    } else {
        Write-Host "Directory $folderPath does not exist."
        return 0
    }
}

# Read the list of usernames
$usernames = Get-Content -Path $userListPath

# Initialize an array to hold the results
$results = @()
$totalSize = 0

# Process each username
foreach ($username in $usernames) {
    $userHomePath = Join-Path -Path $basePath -ChildPath $username
    $folderSize = Get-FolderSize -folderPath $userHomePath
    $totalSize += $folderSize
    $sizeInMB = [math]::Round(($folderSize / 1MB), 2)

   # Create a custom object to store the result

    $result = [PSCustomObject]@{
        Username = $username
        HomeDirectory = $userHomePath
        SizeMB = $sizeInMB
    }

    # Add the result to the array
$results += $result
}

# Calculate the total size in MB
$totalSizeInMB = [math]::Round(($totalSize / 1MB), 2)

# Add the total size to the results
$results += [PSCustomObject]@{
    Username = "Total"
    HomeDirectory = ""
    SizeMB = $totalSizeInMB
}

# Output the results with color coding

foreach ($result in $results) {
    $color = "White"
    if ($result.SizeMB -le 500) {
        $color = "Green"
    } elseif ($result.SizeMB -gt 500 -and $result.SizeMB -le 2500) {
        $color = "Yellow"
    } elseif ($result.SizeMB -gt 2500) {
        $color = "Red"
    }

    if ($result.Username -eq "Total") {
        Write-Host "Total`t`t`t$($result.SizeMB) MB" -ForegroundColor $color
    } else {
        Write-Host "$($result.Username)`t$($result.HomeDirectory)`t$($result.SizeMB) MB" -ForegroundColor $color
    }
}

# Output the results to a file without color coding
$results | Format-Table -AutoSize | Out-File -FilePath $outputPath

The Output

This is what a sample report looks like, as you can see below you have a "green" user and a "red" user then at the bottom you will see the report of the total space required.





Previous Post Next Post

نموذج الاتصال