What It Does
The script lists the user profiles on the PC and removes each one that has not been used for MinAgeDays days: the whole profile folder, with the Desktop, Documents and AppData in it, and its registry entry, the same way Windows deletes a profile. The user gets a fresh profile at their next sign-in. Network and OneDrive copies are not touched.
Windows updates a profile's LastUseTime during servicing, so the script does not trust it. It takes the newer of the profile's NTUSER.DAT write time and the load time Windows keeps in the registry, and only falls back to LastUseTime when neither exists. Each row says which date it used. A profile with no date at all is kept.
Only real user profiles are considered. Loaded profiles, service and virtual profiles, the account running the script, the built-in Administrator, defaultuser0, anyone in ExcludeUsers and, unless IncludeAdmins is true, every direct member of the local Administrators group are never removed.
Before You Run It
- Run the dry run and read every name on the list. A removed profile cannot be recovered.
- Put domain service accounts and managed service accounts in ExcludeUsers. They look like ordinary users to the script.
- Members of a domain group that is itself a local administrator are not detected. Name those users in ExcludeUsers too.
- Check that anything users keep only on the local Desktop or Documents is saved somewhere else before you approve the run.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: High. It is a dry run unless you set both DryRun=false and Approve=remove-stale-user-profiles, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
- What it changes: Deletes unused user profiles: the profile folder and its registry entry.
- Restart: It never restarts the machine.
- Typical run: 1 to 15 minutes, depending on profile size.
- Example answer: "Removed 3 of 3 stale profiles; freed 41.0 GB"
- Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- MinAgeDays: Remove profiles unused for at least this many days. Whole number, 7 to 3650. Default 90.
- ExcludeUsers: Profiles to keep, as profile folder names or account names, separated by commas or semicolons; * and ? wildcards work. Default empty.
- IncludeAdmins: true to also remove stale profiles of local administrators (Administrator and the account running the script are still kept). Default false.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.