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:
- 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.
- 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.
- 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:
- Open Task Scheduler by searching for it in the Start menu.
- Click on “Create Task” in the Actions pane on the right.
- Give the task a name and description, e.g., “Delete Old User Profiles”.
- Under the Triggers tab, click “New” and specify the schedule (e.g., daily, weekly) and the desired time for the task to run.
- Under the Actions tab, click “New” and choose “Start a program” as the action.
- In the “Program/script” field, enter
powershell.exe. - In the “Add arguments” field, enter -ExecutionPolicy Bypass -File “C:\Bijay\DeleteOldProfiles.ps1”, replacing the path with the actual location of your script.
- 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:
- Get All Files in a Directory Using PowerShell
- The File Is Not Digitally Signed You Cannot Run This Script On The Current System in PowerShell
- Create JSON Files with Content Using PowerShell
- Find Text Patterns with PowerShell Select-String
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.