Remove User Profiles Via Dialog

by Jan 15, 2018

We’ve received massive feedback on our tips dealing with user profile management, so we decided to add a couple of additional tips.

In the previous tip we illustrated how WMI can delete user profiles. Some users recommended to use Remove-WmiObject instead of the internal WMI method Delete(). However, it turned out that Remove-WmiObject cannot delete user profile instances.

The code below sums up all the details we published in previous tips. It lists all user profiles that are neither currently loaded nor bound to a system account. You can then select one, and PowerShell will remove the user profile.

Note that the script below is *not* deleting anything. To prevent data loss, we commented out the line that does the deletion. Make sure you understand what it means to delete a user profile before you comment it in and actually remove user profiles!

#requires -RunAsAdministrator

Get-WmiObject -ClassName Win32_UserProfile -Filter "Special=False AND Loaded=False" |
    Add-Member -MemberType ScriptProperty -Name UserName -Value { (New-Object System.Security.Principal.SecurityIdentifier($this.Sid)).Translate([System.Security.Principal.NTAccount]).Value } -PassThru |
    Out-GridView -Title "Select User Profile" -OutputMode Single |
    ForEach-Object {
        # uncomment the line below to actually remove the selected user profile!
        #$_.Delete()
    }

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at www.psconf.eu.

Twitter This Tip! ReTweet this Tip!