How to Delete User Profiles Older Than 30 Days Using PowerShell?

As an IT administrator, managing user profiles on Windows computers is an important task to free up disk space and keep systems running smoothly. Over time, user profiles can accumulate and consume valuable storage, especially on shared devices. In this tutorial, I’ll walk you through how to use PowerShell to automatically delete user profiles older than 30 days.

Why Delete Old User Profiles?

Let’s discuss why deleting old user profiles is beneficial:

  1. Saves Disk Space: User profiles can take up significant storage space, particularly if users store large files or have been using the device for an extended period. You can reclaim valuable disk space by removing profiles that haven’t been accessed in 30 days.
  2. Improves System Performance: Having numerous user profiles on a computer can impact system performance. Windows has to manage and maintain these profiles, which consumes resources. Deleting unused profiles helps optimize performance.
  3. Enhances Security: Inactive user profiles may contain sensitive data that is no longer needed. By deleting these profiles, you minimize the risk of unauthorized access to old user data.

Delete User Profiles Older Than 30 Days Using PowerShell

Now, let’s go through the steps to create a PowerShell script that identifies and deletes user profiles older than 30 days.

Step 1: Identify User Profiles

First, we need to retrieve a list of all user profiles on the computer. We can do this using the Get-CimInstance cmdlet with the Win32_UserProfile class.

$profiles = Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.Special -eq $false }

This command retrieves all user profiles and filters out special profiles like “Default” or “Public”.

Step 2: Calculate Profile Age

Next, we’ll determine the age of each user profile by comparing its last use date with the current date. We can use the LastUseTime property and subtract it from the current date to get the profile age in days.

$currentDate = Get-Date
$daysOld = 30

foreach ($profile in $profiles) {
    $lastUseTime = $profile.LastUseTime
    $profileAge = ($currentDate - $lastUseTime).Days

    if ($profileAge -gt $daysOld) {
        # Delete the profile (code in next step)
    }
}

In this code snippet, we define the $daysOld variable to specify the age threshold (30 days in this case). We then iterate through each profile, calculate its age, and compare it with the threshold.

Step 3: Delete Old User Profiles

Finally, if a profile is older than the specified threshold, we can delete it using the Remove-CimInstance cmdlet.

if ($profileAge -gt $daysOld) {
    $profilePath = $profile.LocalPath
    Remove-CimInstance -InputObject $profile
    Remove-Item -Path $profilePath -Force -Recurse
}

Inside the if block, we retrieve the profile’s local path and use Remove-CimInstance to delete the profile instance. We also remove the profile directory and its contents using Remove-Item.

Step 4: Complete Script

Here’s the complete PowerShell script that combines all the steps:

$profiles = Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.Special -eq $false }
$currentDate = Get-Date
$daysOld = 30

foreach ($profile in $profiles) {
    $lastUseTime = $profile.LastUseTime
    $profileAge = ($currentDate - $lastUseTime).Days

    if ($profileAge -gt $daysOld) {
        $profilePath = $profile.LocalPath
        Remove-CimInstance -InputObject $profile
        Remove-Item -Path $profilePath -Force -Recurse
        Write-Host "Deleted profile: $profilePath (Age: $profileAge days)"
    }
}

I’ve added a Write-Host statement to log the deleted profiles and their age for informational purposes.

Once you run the PowerShell script, this will delete the user profiles older than 30 days.

Check out Create Folders with Year, Month, and Day Using PowerShell

Automate User Profile Cleanup

To automate the process of deleting old user profiles, you can save the PowerShell script with a .ps1 extension (e.g., DeleteOldProfiles.ps1) and schedule it to run regularly using Task Scheduler on Windows.

Here’s how to set up a scheduled task:

  1. Open Task Scheduler by searching for it in the Start menu.
  2. Click on “Create Task” in the Actions pane on the right.
  3. Give the task a name and description, e.g., “Delete Old User Profiles”.
  4. Under the Triggers tab, click “New” and specify the schedule (e.g., daily, weekly) and the desired time for the task to run.
  5. Under the Actions tab, click “New” and choose “Start a program” as the action.
  6. In the “Program/script” field, enter powershell.exe.
  7. In the “Add arguments” field, enter -ExecutionPolicy Bypass -File “C:\Bijay\DeleteOldProfiles.ps1”, replacing the path with the actual location of your script.
  8. Click OK to save the task.

Now, the scheduled task will automatically run the PowerShell script at the specified intervals, keeping your user profiles clean and up to date.

Conclusion

In this tutorial, I explained how to delete user profiles older than 30 days using PowerShell. Remember to test the script in a non-production environment first and adjust the age threshold ($daysOld) according to your organization’s requirements.

Do let me know if this PowerShell script works for you.

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.