How to delete user profile remotely using PowerShell script

Managing user profiles on Windows machines, especially in a networked environment, can be streamlined using PowerShell scripts. Whether you need to clean up old profiles or manage resources efficiently, PowerShell offers powerful tools to handle these tasks remotely. Here’s an updated guide using the latest PowerShell commands:

Identifying Unused Profiles

To start, we identify user profiles that have not been used for a specified period. This script lists user profiles not accessed in the past 60 days:

$CutoffDate = (Get-Date).AddDays(-60) Get-CimInstance -ClassName Win32_UserProfile | Where-Object { -not $_.Special -and $_.LastUseTime -and [datetime]::FromFileTime($_.LastUseTime) -lt $CutoffDate } | Select-Object LocalPath, LastUseTime

Safely Removing Profiles

To remove these profiles, we integrate the Remove-CimInstance command. Always use the -WhatIf parameter initially to simulate the removal without making any changes:

Get-CimInstance -ClassName Win32_UserProfile | Where-Object { -not $_.Special -and -not $_.Loaded -and $_.LastUseTime -and [datetime]::FromFileTime($_.LastUseTime) -lt $CutoffDate } | Remove-CimInstance -WhatIf

Excluding Specific Users

To prevent the deletion of certain user profiles (like system accounts or specific user accounts), modify the script as follows:

$ExcludedUsers = @("Public", "zenoss", "svc", "user_1", "user_2") Get-CimInstance -ClassName Win32_UserProfile | Where-Object { -not $_.Special -and -not $_.Loaded -and -not $ExcludedUsers.Contains($_.LocalPath.Replace("C:\Users\", "")) -and $_.LastUseTime -and [datetime]::FromFileTime($_.LastUseTime) -lt $CutoffDate } | Remove-CimInstance -WhatIf

Automating Profile Deletion

To automate the process, consider adding this script to a Group Policy shutdown script or scheduling it with Task Scheduler. However, ensure thorough testing in your environment before implementing automatic deletion.

Handling Users in Specific AD Groups

You can adapt this script to automatically delete profiles of users in a particular AD group. This can be useful for managing profiles post-employment or in specific scenarios:

$ADGroup = 'ResignedUsers' $CutoffDate = (Get-Date).AddDays(-60) $users = Get-ADGroupMember -Identity $ADGroup | ForEach-Object { $_.Sid.Value } $profiles = Get-CimInstance Win32_UserProfile $profiles | Where-Object { $users -contains $_.SID -and [datetime]::FromFileTime($_.LastUseTime) -lt $CutoffDate } | Remove-CimInstance -WhatIf


Using these updated PowerShell scripts, you can efficiently manage user profiles on remote Windows systems. Remember to conduct thorough testing and ensure backups are in place to prevent data loss. PowerShell continues to be a robust tool for system administrators, offering scalable solutions for various network management tasks.

Mohammed Nihal
Latest posts by Mohammed Nihal (see all)

Was this Article Helpful?

Did I just helped you solve one of your problem? Support me by buying me a coffee. Thanks for your support