Removing User Profiles Via Dialog (Part 2)

by Jan 16, 2018

In the previous tip we illustrated how a grid view window can display all available user profiles, lets you select one, and deletes it:

#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()
    }

While it works as expected, the grid view window shows a lot of information that is not needed. If you’d like to use it as a service desk tool, you’d really only want to show a fraction of that data.

So of course you could use Select-Object before you pipe results into a grid view window to control the information shown. However, this would change the object type, and it would no longer be possible to access object members such as Delete() to delete a user profile.

So this is a more general problem:

How can a grid view window show custom data, yet when the user selects an item, returns the original object?

One simple approach is a hash table that is created for you by Group-Object: group the original data by a property like “UserName”. Then, show the hash table keys in the grid view window. Once the user selected an item, use the selection as key to the original item in your hash table:

#requires -RunAsAdministrator

$hashTable = 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 |
    Group-Object -Property UserName -AsHashTable -AsString

$hashTable.Keys |
    Sort-Object |
    Out-GridView -Title "Select User Profile" -OutputMode Single |
    ForEach-Object {
        # uncomment the line below to actually remove the selected user profile!
        # $hashTable[$_].Delete()

Now the tool is a lot easier to use: the grid view window shows the user names only, and once you select one, the original object can be retrieved to do the actual deletion.

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!