# PowerShell to Remove bad profile folders

**URL:** <https://forums.powershell.org/t/powershell-to-remove-bad-profile-folders/18826>\
**Category:** PowerShell Help\
**Created:** [March 15, 2022, 11:40am UTC](https://forums.powershell.org/t/powershell-to-remove-bad-profile-folders/18826 "2022-03-15T11:40:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samec\_Brian\_D](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@Samec\_Brian\_D](https://forums.powershell.org/u/Samec_Brian_D)\
**Post date:** [March 15, 2022, 11:40am UTC](https://forums.powershell.org/t/powershell-to-remove-bad-profile-folders/18826/1 "2022-03-15T11:40:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 15, 2022, 12:14pm UTC](https://forums.powershell.org/t/powershell-to-remove-bad-profile-folders/18826/2 "2022-03-15T12:14:40Z")

</div>

Brian D,  
Welcome to the forum. 👋🏽

> [@Samec\_Brian\_D](#):
>
> Looking for a PowerShell script to remove only bad profile folders.

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 …

> [@Samec\_Brian\_D](#):
>
> 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.

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:

> **[Delete User Profiles with PowerShell & CIM](https://adamtheautomator.com/powershell-delete-user-profile/)**
>
> Use PowerShell and the CIM command to delete user profiles on Windows systems in this step-by-step guide.

---

<div class="post-metadata">

**Author:** ![Kira-Shadow](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@Kira-Shadow](https://forums.powershell.org/u/Kira-Shadow)\
**Post date:** [March 16, 2022, 12:31am UTC](https://forums.powershell.org/t/powershell-to-remove-bad-profile-folders/18826/3 "2022-03-16T00:31:49Z")

</div>

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 🙂)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:18pm UTC](https://forums.powershell.org/t/powershell-to-remove-bad-profile-folders/18826/4 "2024-05-16T20:18:12Z")

</div>

Closed on May 16, 2024, 8:18pm UTC


