fb
Homepage 5 How-to Articles 5 How To Delete User Profiles Remotely with PowerShell

How To Delete User Profiles Remotely with PowerShell

Manage remote endpoints, deploy software and patches with Action1 cloud RMM solution.

Sign up and use free on up to 50 endpoints with no functionality limits or expiration.



December 1, 2020
When a user logs onto the computer for the first time (not via the network to access shared folders or printers), Windows creates a user profile. Among its contents are the NTUSER.DAT file (user profile settings), user-specific folders (My Documents, Desktop, etc.), and Application Data. 

When it comes to cleaning up the C:\Users directory of old and obsolete user profiles on Windows workstations and servers, especially RDS (Remote Desktop Services) terminal servers, you can remotely delete a user profile from the command line tool using PowerShell.

As for the terminal servers, in the C:\Users folder, there accumulates a huge number of directories with out-of-date user profiles on a regular basis. Setting quotas on a directory and thus limiting the total amount of storage consumed on the directory (using FSRM or NTFS quotas, roaming folders, etc.) can be somewhat of a solution. In this article, we’ll cover the question of how to delete a user profile remotely via PowerShell.

How to Remove Old User Profiles via Powershell

Use a PowerShell script to find and delete user profiles for inactive or blocked users.

STEP 1: Calculate the size of the profile of each user in the C:\Users folder with the following script:

gci -force ‘C:\Users’-ErrorAction SilentlyContinue | ? { $_ -is [io.directoryinfo] } | % {

$len = 0

gci -recurse -force $_.fullname -ErrorAction SilentlyContinue | % { $len += $_.length }

$_.fullname, ‘{0:N2} GB’ -f ($len / 1Gb)

$sum = $sum + $len

}

“Total size of profiles”,'{0:N2} GB’ -f ($sum / 1Gb)

For instance, in our case the total size of all user profiles in the C:\Users directory is about 22 GB (see below).

total size of all user profiles

STEP 2: List users whose profiles haven’t been active for more than 60 days. Use the value of the LastUseTime profile field for searching:

Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-60))}| Measure-Object

Below, we’ve got 143 inactive user profiles on the terminal server.

inactive user profiles on the terminal server

STEP 3: To remove all of these user profiles.

  • You can add a redirect list to the Remove-WmiObject command (before using the script to delete old user profiles, we recommend to double-check its output using the -WhatIf parameter):

Get-WMIObject -class Win32_UserProfile | Where {(! $ _. Special) -and (! $ _. Loaded) -and ($ _. ConvertToDateTime ($ _. LastUseTime) -lt (Get-Date) .AddDays (-30))} | Remove-WmiObject –WhatIf

  • To exclude some profiles — e.g. special System and Network Service accounts, a local administrator account, users with active sessions, etc. — you need to modify the script as follows:

# List of accounts whose profiles cannot be deleted

$ExcludedUsers =”Public”,”zenoss”,”svc”,”user_1”,”user_2”

$LocalProfiles=Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and (!$_.Loaded) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-60))}

foreach ($LocalProfile in $LocalProfiles)

{

if (!($ExcludedUsers -like $LocalProfile.LocalPath.Replace(“C:\Users\”,””)))

{

$LocalProfile | Remove-WmiObject

Write-host $LocalProfile.LocalPath, “profile deleted” -ForegroundColor Magenta

}

}

NOTE: Before running an automatic cleanup of old user profiles, thoroughly examine the script and test it within your environment! You can schedule the run of the script via GPO shutdown scripts or Task Scheduler.

  • To automatically delete all users added to a specific AD group (for example, the DisabledUsers group), modify the script:

$users = Get-ADGroupMember -Identity DisabledUsers | Foreach {$_.Sid.Value}

$profiles = Get-WmiObject Win32_UserProfile

$profiles | Where {$users -eq $_.Sid} | Foreach {$_.Delete()}

How to Remove User Profile in Windows with Action1

Action1’s intuitive dashboard helps optimize routine tasks, significantly scaling up IT productivity.

Step 1: After logging into the Action1 dashboard, in the Navigation pane (the left column), select Managed Endpoints and mark the endpoint to delete the user profile remotely.

managed endpoints kb

Step 2: Then click on the More Actions menu and select Run Command.

run command kb

Step 3: In the box, type the script:

$Computer = Read-Host “Please Enter Computer Name: “

$user = Read-Host “Enter User ID: “

Invoke-Command -ComputerName $computer -ScriptBlock {

param($user)

$localpath = ‘c:\users\’ + $user

Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq $localpath} |

Remove-WmiObject

} -ArgumentList $user

Delete User Profil script Action1

Step 4: In the Select Managed Endpoints window, you mark those endpoints on which you are going to change the time zone. You can add all the available endpoints or mark them one by one. 

select managed endpoints kb

Step 5: Schedule the action (Run now/ No schedule yet/ At specific time/ Repeat) and Finish.

schedule and finish kb

Action1’s Remote Management Solutions

Staying competitive in the market is always a challenge, and loud words don’t do wonders for optimizing administrative tasks and scaling up IT productivity. But actions do! With Action1’s cloud-based RMM solution, your IT department will timely deliver patches and updates, manage IT assets, maintain remote desktop, and run many other complex tasks.

Read the TechRadar review of our product or be the judge — get your free trial.

Related Articles

How to Uninstall Programs With Standard Windows Tools

UThis tutorial for new users details where to add and remove Windows 10 programs, the quickest way to get to this Control Panel component, and more information on how to properly uninstall Windows 10 programs and applications from your computer. In fact, when compared...

How to Shutdown Computers via Powershell Remotely

It is logical that most people will always use graphic windows and a mouse to complete work on the computer, this is correct since it is simpler and faster, but there are a number of cases when you, as an advanced user or a system administrator, are simply obliged to...

How to Upgrade Windows 10 Build from the Command Line

The setup.exe installer in the Windows 10 distribution has a fairly large set of command-line options that may be of interest to the administrator for automating and managing updating the build (version / build) of Windows 10 on users computers using SCCM and other...

About Action1 RMM

Action1 RMM is a cloud-based IT solution for remote monitoring and management, patching, and remote support.

Start your free two-week trial of Action1, or use RMM tools for free forever on 50 endpoints with no functionality limitations!



0 Comments

Submit a Comment

Your email address will not be published.

cloud patch management solutions action1

RMM Solution for MSPs

Centralize endpoint management and boost efficiency of IT service delivery.

automated server patch management action compliance

Patch Management

Identify and deploy missing OS and third-party software updates.

cloud software deployment tools windows

Software Deployment

Distribute software and updates across managed endpoints.

software distribution tools software inventory action1

IT Asset Inventory

Keep a detailed inventory and manage hardware and software assets.

web client remote desktop

Remote Desktop

Support users via seamless remote desktop connection.

web based rdp client

Unattended Access

Provide administrative support and manage remote devices.

automated patch management action1

Endpoint Management

Run PowerShell, custom scripts, reboot computers and restart services.

API integrations action1

RESTful API

Integrate Action1 RMM to your IT ecosystem.

computer inventory tool for compliance

Reports and Alerts

Conduct endpoint security audits with comprehensive reporting.