How do I delete old user profiles

Dipesh Neupane 0 Reputation points
2024-03-26T16:11:31.5066667+00:00

How do I delete old user profiles on my Dell Latitude 3540 running Windows 11 Enterprise (Version: 22H2, OS Build: 22621.3155) using PowerShell? I need to free up space on multiple machines at the school where I work as an IT Technician. I want to delete only the old user profiles in bulk without affecting the Admin profiles or Active Directory

Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,666 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Marcin Policht 24,450 Reputation points MVP
    2024-03-26T16:39:31.11+00:00

    You can try the following (for profiles that haven't been updated in 6 months):

    # Define the threshold date (6 months ago)
    $thresholdDate = (Get-Date).AddMonths(-6)
    # Get a list of user profiles excluding the Administrator profile
    $userProfiles = Get-CimInstance Win32_UserProfile | Where-Object { $_.Special -ne $true -and $_.LocalPath -notlike "*\\Administrator" } | Select-Object LocalPath, SID, LastUseTime
    # Filter profiles that haven't been updated in the last 6 months
    $profilesToDelete = $userProfiles | Where-Object { $_.LastUseTime -lt $thresholdDate }
    # Delete the identified profiles
    foreach ($profile in $profilesToDelete) {
        Write-Host "Deleting profile: $($profile.LocalPath)"
        Remove-CimInstance -ClassName Win32_UserProfile -Filter "SID='$($profile.SID)'"
    }
    
    
    

    Save this script as a .ps1 file, such as DeleteOldUserProfiles.ps1. Then, you can run this script periodically using Task Scheduler or another automation tool to automate the process.

    Make sure to test the script in a safe environment before deploying it to production. Additionally, ensure that you run the script with administrative privileges to delete user profiles successfully.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


  2. Ian Xue 37,106 Reputation points Microsoft Vendor
    2024-03-28T07:08:21.9866667+00:00

    Hi Dipesh Neupane,

    Please try this script.

    $localadmin = Get-LocalGroupMember -Group administrators
    $date = (Get-Date).AddDays(-30)
    Get-CimInstance -ClassName Win32_UserProfile | Where-Object {$_.sid -notin $localadmin.sid.value -and $_.LastUseTime -lt $date} | ForEach-Object{ Remove-CimInstance -InputObject $_ }
    

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Gary Blok 1,736 Reputation points
    2024-03-28T13:26:52.76+00:00

    I've had good success with Delprof2 in the past and have added into several different cleanup processes over the years: https://helgeklein.com/free-tools/delprof2-user-profile-deletion-tool/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.