PowerShell to Remove bad profile folders

Looking for a PowerShell script to remove only bad profile folders. By bad, I mean, the user profile does not appear any longer in the profiles list under Advanced Settings - User Profiles in Windows, but a folder still exists under C:\Users for that username. The folder may contain a couple files or a OneDrive folder that didn’t delete when the profile itself was removed. So would want to be able to somehow search existing profiles and then delete any folders under C:\Users that didn’t correspond to an existing profile on the system.

Not sure how to approach doing this, or if possible.

Thank you.

Brian D,
Welcome to the forum. :wave:t4:

This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

Anyway, to get you started …

You could try different ways of enumerating user profiles and try to find if there’s a difference.
One way is obviously to list the file system folders. Another way is listing the user profiles with WMI/CIM.

Here you can read more about:

If you are looking to delete redundant profile folders from c:\users , the key is how to identify current profiles, I would suggest looking in the registry under Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList under each ssid there is a ProfileImagePath value.
I would use these to identify the profile folders that are needed and hence the others can be deleted safely. (not powershell I know but its a starter :slightly_smiling_face:)