Public/Get-FslogixDirectorySize.ps1
<#
.SYNOPSIS Lists recent directories and their sizes for user folders in a network share based on user inputs. .DESCRIPTION This function accepts a single username, a text file, or a CSV file containing usernames. It computes their SIDs, checks for directories in each user's folder on a network share that were created within the last 24 hours, calculates the sizes of these directories, and outputs the name and size. .PARAMETER UserInput A single username, the path to a text file containing usernames, or the path to a CSV file. .PARAMETER UserInputType Specifies the type of the UserInput: 'Single', 'TextFile', or 'CSV'. Default is 'Single'. .PARAMETER BaseSharePath The base UNC path to the network share where user folders are located. .EXAMPLE PS> Get-RecentDirectorySizes -UserInput "jdoe" -BaseSharePath "\\afs001.test.local\w11-fsl" Checks the directory size for user 'jdoe' in the specified network share. .EXAMPLE PS> Get-RecentDirectorySizes -UserInput "C:\Users\userlist.txt" -UserInputType "TextFile" -BaseSharePath "\\afs001.test.local\w11-fsl" Reads usernames from 'userlist.txt' and checks their directory sizes in the specified network share. .EXAMPLE PS> Get-RecentDirectorySizes -UserInput "C:\Users\userlist.csv" -UserInputType "CSV" -BaseSharePath "\\afs001.test.local\w11-fsl" Reads usernames from 'userlist.csv' and checks their directory sizes in the specified network share. .NOTES Requires permissions to read the user file and access the network share. Assumes CSV files have a column named 'Username'. #> Function Get-FslogixDirectorySize { [CmdletBinding()] Param( [Parameter(Mandatory = $true)] [string]$UserInput, [Parameter(Mandatory = $false)] [ValidateSet("Single", "TextFile", "CSV")] [string]$UserInputType = "Single", [Parameter(Mandatory = $true)] [string]$BaseSharePath ) Process { $usernames = switch ($UserInputType) { "Single" { @($UserInput) } "TextFile" { Get-Content -Path $UserInput } "CSV" { Import-Csv -Path $UserInput | ForEach-Object { $_.Username } } } foreach ($username in $usernames) { $sidValue = (New-Object System.Security.Principal.NTAccount($username)).Translate([System.Security.Principal.SecurityIdentifier]).Value $userPath = Join-Path -Path $BaseSharePath -ChildPath "${sidValue}_$username" if (Test-Path $userPath) { $recentDirectories = Get-ChildItem -Path $userPath -Filter "*.vhdx" foreach ($dir in $recentDirectories) { $dirSizeBytes = (Get-ChildItem -Path $dir.FullName -Recurse -File | Measure-Object -Property Length -Sum).Sum $dirSizeGB = $dirSizeBytes / 1GB $roundedValue = [Math]::Round($dirSizeGB, 2) Write-Output "Directory: $($dir.Name), Size: $($roundedValue) GB" } } else { Write-Warning "Profile does not exist in $BaseSharePath for user $username. Continuing to next user." } } } } |