HOW TO: Delete Unused User Profile in Windows 7

This script will requires Windows Power Shell version 3

#requires -version 3.0
 
#Remove-UserProfile.ps1
 
[cmdletbinding(SupportsShouldProcess)]
 
Param(
[Parameter(Position=0)]
[ValidateNotNullorEmpty()]
[int]$Days=30
)

Function isException($Foldername)
{
	Switch($Foldername)
	{
		"C:\Users\All Users"
		{ $True} 
		"C:\Users\Default User" 
		{ $True }
		"C:\Users\Default" 
		{ $True }
		"C:\Users\LocalService" 
		{ $True }
		"C:\Users\NetworkService" 
		{ $True }
		"C:\Users\Administrator" 
		{ $True }
		"C:\Users\Administrator.SAO13" 
		{ $True }
		"C:\Users\Deploy_admin"
		{ $True }
		C:\Users\"Deploy_admin.SAO"
		{ $True }
		"C:\Users\Deploy_admin.SAO13"
		{ $True }
		"C:\Users\Pham_R"
		{ $True }
		"C:\Users\Adm-Pass" 
		{ $True }
		"C:\Users\AppData" 
		{ $True }
		"C:\Users\Classic .NET AppPool" 
		{ $True}
		"C:\Users\DefaultAppPool" 
		{ $True}
		"C:\Users\Public" 
		{ $True}
		"C:\Windows\ServiceProfiles\LocalService" 
		{ $True}
		"C:\Windows\ServiceProfiles\NetworkService" 
		{ $True}
		"C:\Windows\system32\config\systemprofile"
		{ $True}
		default 
		{ $False}
	}
}
 
Start-Transcript -Path C:\ProfileCleanup.txt -Append
 
Write-Warning "Filtering for user profiles older than $Days days" 
Get-CimInstance win32_userprofile -Verbose | 
	Where {$_.LastUseTime -lt $(Get-Date).Date.AddDays(-$days) -and (isException $_.LocalPath) -eq $False} | 
	Remove-CimInstance -Verbose

Stop-Transcript

To check your Windows Power Shell version, you can go to your Windows Power Shell and type the following

$PSVersionTable

Windows Management Framework 3.0 can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=34595

 

Comments are closed