One of my clients recently wanted us to delete old and unused user profiles from Windows 11 as they took up valuable disk space. You can manage user profiles using PowerShell. In this tutorial, I will explain how to delete user profiles using PowerShell in Windows 11.
Note: Ensure you have administrative access to the Windows 11 machine.
Method 1: Using the System Settings
The simplest way to delete a user profile in Windows 11 is through the System Settings. Here’s how:
- Click on the Start menu and select the Settings app.
- Navigate to “System” and then click on “Storage.”
- Under “Storage management,” click on “User Profiles.”
- Select the profile you wish to delete and click on “Delete.”
However, keep in mind that this method may not always work, especially if the user profile is corrupted or locked. In such cases, you can use PowerShell.
Check out Generate SSH Keys with PowerShell
Method 2: Using PowerShell
Now, let me show you how to use PowerShell to delete user profiles in Windows 11. Follow these steps:
- Open the Start menu, type “PowerShell,” right-click on “Windows PowerShell,” and select “Run as administrator.”
- In the PowerShell window, type the following command and press Enter:
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'USERNAME' } | Remove-CimInstanceReplace ‘USERNAME’ with the actual username of the profile you want to delete. For example, if the username is ‘Bijay,’ the command would be:
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'Bijay' } | Remove-CimInstanceThis command uses the CIM (Common Information Model) cmdlets to retrieve the user profile information and delete the specified profile.
- If the user profile is currently loaded, you may encounter an error. In such cases, you can force the deletion by adding the
-Forceparameter:
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'USERNAME' } | Remove-CimInstance -ForceBe cautious when using the -Force parameter, as it will delete the user profile without any prompts or confirmation.
Check out Show Logged-In Users with PowerShell
Method 3: Using a PowerShell Script
Sometimes, you might need to perform bulk operations, such as deleting multiple user profiles in Windows 11 with a PowerShell script.
Here’s an example script that deletes user profiles based on a specific condition:
$UserProfiles = Get-CimInstance -Class Win32_UserProfile
$DaysOld = 90
foreach ($UserProfile in $UserProfiles) {
$LastUsed = $UserProfile.LastUseTime
if ($LastUsed -lt (Get-Date).AddDays(-$DaysOld)) {
$UserProfile | Remove-CimInstance
Write-Host "Deleted user profile: $($UserProfile.LocalPath)"
}
}This script does the following:
- It retrieves all user profiles using the
Get-CimInstancecmdlet. - It defines a variable
$DaysOldto specify the number of days (in this case, 90) after which a user profile should be considered old and eligible for deletion. - It iterates through each user profile and checks the
LastUseTimeproperty. - If the
LastUseTimeis older than the specified number of days ($DaysOld), the script deletes the user profile using theRemove-CimInstancecmdlet and displays a message indicating the deleted profile.
You can save this script with a .ps1 extension (e.g., DeleteOldProfiles.ps1) and run it in PowerShell with administrative privileges.
Check out How to Remove a Computer from a Domain Using PowerShell
Delete a Single User Profile by Name using PowerShell
To delete a single user profile by name using PowerShell, follow these steps:
- Open the Start menu, type “PowerShell,” right-click on “Windows PowerShell,” and select “Run as administrator.”
- In the PowerShell window, type the following command and press Enter:
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'USERNAME' } | Remove-CimInstanceReplace ‘USERNAME’ with the actual username of the profile you want to delete. For example, if the username is ‘Bijay,’ the command would be:
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'Bijay' } | Remove-CimInstanceThis command uses the CIM (Common Information Model) cmdlets to retrieve the user profile information and delete the specified profile.
- If the user profile is currently loaded, you may encounter an error. In such cases, you can force the deletion by adding the
-Forceparameter:
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'USERNAME' } | Remove-CimInstance -ForceBe cautious when using the -Force parameter, as it will delete the user profile without any prompts or confirmation.
Check out Add a Computer to a Domain Using PowerShell
Delete Multiple User Profiles by Name using PowerShell
If you need to delete multiple user profiles based on their names, you can modify the PowerShell command to include multiple usernames. Here’s an example:
$UserNames = @('JohnDoe', 'JaneSmith', 'BobJohnson')
foreach ($UserName in $UserNames) {
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq $UserName } | Remove-CimInstance
}In this example:
- We define an array
$UserNamesthat contains the usernames of the profiles we want to delete. - We use a
foreachloop to iterate through each username in the$UserNamesarray. - For each username, we execute the
Get-CimInstancecommand to retrieve the user profile information and delete the profile usingRemove-CimInstance.
You can modify the $UserNames array to include the specific usernames you want to delete.
Check out Set the Default Printer Using PowerShell in Windows
Delete User Profiles Based on a Pattern in PowerShell
In some cases, you may want to delete user profiles based on a specific pattern in their names. PowerShell allows you to use wildcards to match patterns. Here’s an example:
$UserNamePattern = "Temp*"
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath -like "*$UserNamePattern" } | Remove-CimInstanceIn this example:
- We define a variable
$UserNamePatternthat contains the pattern we want to match. In this case, it’s “Temp*,” which will match any username starting with “Temp.” - We use the
Get-CimInstancecommand to retrieve all user profiles. - We pipe the results to the
Where-Objectcmdlet and use the-likeoperator to filter the profiles based on the$UserNamePattern. The*wildcard matches any characters after “Temp.” - Finally, we pipe the filtered profiles to
Remove-CimInstanceto delete them.
You can adjust the $UserNamePattern variable to match your desired pattern.
Conclusion
In this tutorial, I explained how to delete user profiles in Windows 11 using PowerShell. This will help you manage your system’s storage and performance. Do let me know in the comments below if you face any issues.
You may also like:
- How to Remove a Computer from a Domain Using PowerShell
- Set Password Never Expires for Local User Using PowerShell
- Set Password for Local User in Windows 11 Using PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.