receives compensation from some of the companies listed on this page. Advertising Disclosure
BDC Hamburger Icon


BDC Logo
Search Icon
Updated Jul 05, 2023

How to Manage User Profiles With PowerShell

author image
Adam Bertram, Senior Writer & Expert on Business Operations

Table of Contents

Open row

A common pain point in an information technology (IT) administrator’s career is managing user profiles. User profiles are a ubiquitous part of a Windows IT pro’s life — especially those that manage virtual desktop environments like Remote Desktop Services.

IT pros have to deal with corrupted user registry hives and files that need to be shared across all user profiles. They also need to figure out how to recreate corrupt profiles and so on. What was once a frustrating experience has gotten a little less so with PowerShell. Here are a few ways that PowerShell can make managing Windows user profiles easier.

Enumerating user profiles

It’s easy to take a peek at user profiles on the file system on a single Windows computer. Look in the C:Users folder. However, when you do this, not only are you not getting the full picture, but it’s also troublesome due to potential file system access problems. There’s a better way and that’s through Windows Management Instrumentation (WMI). 

TipBottom line

Don’t just rely on file system access to view user profiles. The WMI class provides a far more comprehensive and detailed view, similar to the greater control and insight you get when using PowerShell to manage Internet Information Services application pools.

In WMI, a class exists called Win32_UserProfile. This class holds user profile information on Windows systems. It contains all of the profiles that exist on a machine and lots of other useful information that a simple file system folder won’t show you. Together with PowerShell modules, you can enumerate, create or delete user profiles. 

Using PowerShell you can access this WMI class with the Get-CimInstance or Get-WmiObject cmdlets. In Figure 1 below, you find the first user profile on the local computer. You’ll notice many useful tidbits of information, such as LastUseTime and SID. From here, you can also drill down further and get specific paths like Desktop, Documents and Favorites.

PowerShell user profile

Figure 1

Since this is part of WMI, you can easily extend this from a single computer to many computers using the ComputerName parameter. To be compatible with down-level operating systems, you can use the Get-WmiObject cmdlet here to enumerate user profiles on the MEMBERSRV1 and CLIENT2 computers.

Get-WmiObject -Class Win32_UserProfile -ComputerName ‘MEMBERSRV1′,’CLIENT2’

Removing user profiles

Another common task when managing user profiles is removing them. This is sometimes necessary because files can become corrupted. After removal, you need the user to log in again and recreate their profile. Sometimes, you might have the user log off and remove the C:Users<UserName> folder from the file system. Usually, this works, but sometimes it doesn’t. The problem with this technique is that it leaves some remnants behind. The proper way to do this — and the easier way — is to initiate a removal via WMI.

Using the same WMI class, it’s possible to not only just view profiles but completely remove them as well. This is the same as going into the User Profiles box under System settings and hitting the Delete button.

Did You Know?Did you know

Using PowerShell is often far more efficient than using System settings. Many programmers prefer to use PowerShell for system management tasks like installing Windows patches for that efficiency.


PowerShell user profiles

To do this, enumerate the user profiles again and this time apply a filter to pick a single user profile to remove. In this case, remove the user profile called Administrator.CLIENT1. You can do this by using PowerShell’s Where-Object cmdlet and some string manipulation to grab the user folder name from the LocalPath property — that section is shown below in bold.

Get-WmiObject -Class Win32_UserProfile | where {$_.LocalPath.split(”)[-1] -eq ‘Administrator.CLIENT1’} | foreach {$_.Delete()}

Once you’re able to narrow down that single profile, you can then call the Delete() method for each object that Get-WmiObject outputs — in this case only 1 — which will then remove the user profile from the file system as well as the registry.

Get-WmiObject -Class Win32_UserProfile | where {$_.LocalPath.split(”)[-1] -eq ‘Administrator.CLIENT1’} | foreach {$_.Delete()}

Again, if you’d like to extend this to multiple computers, you’d use the –ComputerName parameter on Get-WmiObject.

Get-WmiObject -Class Win32_UserProfile –ComputerName CLIENT1,CLIENT2 | where {$_.LocalPath.split(”)[-1] -eq ‘Administrator.CLIENT1’} | foreach {$_.Delete()}

Bottom LineBottom line

Simplify tasks like enumeration and user profile removal by using WMI in combination with PowerShell. You get much better control on a granular level, much like the enhanced control you get with programming loops when manipulating data.

Use WMI, not the file system

You’ve now seen an easy way to enumerate and remove Windows user profiles. If you weren’t aware of the WMI class Win32_UserProfile you may have been correlating the C:Users<Username> folder as the profile. Now, you can see there’s much more to the user profile than a simple file system folder. Use WMI the next time you need to query or remove user profiles from computers in your environment.

Mark Fairlie contributed to this article.

author image
Adam Bertram, Senior Writer & Expert on Business Operations
Adam Bertram is an IT expert and business owner who has spent decades advising on network administration and security, designing and building infrastructure, and creating and teaching courses on Windows Server, Powershell and more. While maintaining his own IT business, he has provided hands-on DevsOps services for clients like JPMorgan Chase. Bertram, who has a degree in computer science, holds Microsoft, Cisco and CompTIA credentials. He has written numerous tutorials, guides and books, including "Building Better PowerShell Code: Applying Proven Practices One Tip at a Time."
BDC Logo

Get Weekly 5-Minute Business Advice

B. newsletter is your digest of bite-sized news, thought & brand leadership, and entertainment. All in one email.

Back to top