How to Delete User Profiles Using PowerShell in Windows 11?

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:

  1. Click on the Start menu and select the Settings app.
  2. Navigate to “System” and then click on “Storage.”
  3. Under “Storage management,” click on “User Profiles.”
  4. 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:

  1. Open the Start menu, type “PowerShell,” right-click on “Windows PowerShell,” and select “Run as administrator.”
  2. In the PowerShell window, type the following command and press Enter:
   Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'USERNAME' } | Remove-CimInstance

Replace ‘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-CimInstance

This command uses the CIM (Common Information Model) cmdlets to retrieve the user profile information and delete the specified profile.

  1. If the user profile is currently loaded, you may encounter an error. In such cases, you can force the deletion by adding the -Force parameter:
   Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'USERNAME' } | Remove-CimInstance -Force

Be 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:

  1. It retrieves all user profiles using the Get-CimInstance cmdlet.
  2. It defines a variable $DaysOld to specify the number of days (in this case, 90) after which a user profile should be considered old and eligible for deletion.
  3. It iterates through each user profile and checks the LastUseTime property.
  4. If the LastUseTime is older than the specified number of days ($DaysOld), the script deletes the user profile using the Remove-CimInstance cmdlet 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:

  1. Open the Start menu, type “PowerShell,” right-click on “Windows PowerShell,” and select “Run as administrator.”
  2. In the PowerShell window, type the following command and press Enter:
   Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'USERNAME' } | Remove-CimInstance

Replace ‘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-CimInstance

This command uses the CIM (Common Information Model) cmdlets to retrieve the user profile information and delete the specified profile.

  1. If the user profile is currently loaded, you may encounter an error. In such cases, you can force the deletion by adding the -Force parameter:
   Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'USERNAME' } | Remove-CimInstance -Force

Be 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:

  1. We define an array $UserNames that contains the usernames of the profiles we want to delete.
  2. We use a foreach loop to iterate through each username in the $UserNames array.
  3. For each username, we execute the Get-CimInstance command to retrieve the user profile information and delete the profile using Remove-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-CimInstance

In this example:

  1. We define a variable $UserNamePattern that contains the pattern we want to match. In this case, it’s “Temp*,” which will match any username starting with “Temp.”
  2. We use the Get-CimInstance command to retrieve all user profiles.
  3. We pipe the results to the Where-Object cmdlet and use the -like operator to filter the profiles based on the $UserNamePattern. The * wildcard matches any characters after “Temp.”
  4. Finally, we pipe the filtered profiles to Remove-CimInstance to 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.