This script automatically removes user profiles in the same way that Windows would, using the Win32_UserProfile WMI instance. This ensures that the next time the user logs on, they will not encounter any errors, as opposed to simply deleting the folder on disk.
Removes user profiles until the desired free space threshold on the disk is reached.
- Example:
-SpaceLimit 100will delete user profiles until you reach 100GB of free space on the disk.
Removes any user profiles that haven't been used since today's date minus the specified number of months.
- Example:
-MonthCutoff 6will remove user profiles that have not been used in the last six months.
You can combine -SpaceLimit and -MonthCutoff to remove user profiles based on both criteria.
- Example:
-SpaceLimit 100 -MonthCutoff 6will remove user profiles that have not been used in the last six months, until you have 100GB of free space on the disk.
Whitelists any matching username from deletion.
- Example:
-WhitelistUser USDOEJwill not delete theUSDOEJuser regardless of space or month limitations. This is useful for admin profiles, service accounts, or top users. - Note: If you are going to use this for an organization, you can also directly change the
$WhitelistUsersvariable (added 's') so you don't have to check this switch every time.
Limits the number of profiles removed (used for testing).
- Example:
-ProfileLimit 5will only remove the first 5 found profiles, in arbitrary order.
Enables "safe" mode, which bypasses admin checks and does not actually delete any user profiles.
Enables verbose logging for some commands (WMI related commands).
-
Basic Usage:
.\UserProfileCleanup.ps1 -SpaceLimit 100 -MonthCutoff 6
-
Whitelist a User:
.\UserProfileCleanup.ps1 -SpaceLimit 100 -MonthCutoff 6 -WhitelistUser USDOEJ
-
Test Run (Debug Mode):
.\UserProfileCleanup.ps1 -SpaceLimit 100 -MonthCutoff 6 -DebugMode
-
Verbose Logging:
.\UserProfileCleanup.ps1 -SpaceLimit 100 -MonthCutoff 6 -Verbose
- Ensure you have the necessary permissions to run this script.
- Use
-DebugModeto test the script without making any changes. - The
-Verboseswitch can help with troubleshooting by providing detailed logs.