This PowerShell script sample shows how to delete user profiles older than a specified number of days.
The computer usually used by more than one person, this often produces a large number of unique user profiles. Some of profiles are needless. (Profiles that have not been accessed in the past 60 days), users would like to clean up these legacy profiles. If we can provide a script that can help user to delete user profiles that is older than a specified number of days, it would be very helpful.
Step 1: Click Start, type powershell in the search box on the Start Menu, right-click the Windows PowerShell icon, and then click Run Windows PowerShell as administrator. If the User Account Control dialog box appears, confirm that the action it displays is what you want, and then click Continue.
Step2: Start the PowerShell Console with administrator. To run the script in the Windows PowerShell Console, type the command< Script Path> at the Windows PowerShell Console.
For example, type C:\Script\RemoevLocalUserProfile.ps1
This is shown in the following figure.

Step 3: This Script include several parameters. You can type the command Get-Help < Script
Path> -Full to display the entire help file for this function, such as the syntax, parameters, or examples.
Example 1: Type C:\Script\RemoveLocalUserProfile.ps1 -ListUnusedDay 1 command in the Windows PowerShell Console.

This command will list of unused more than 1 days of user profile.
Example 2: Type C:\Script\RemoveLocalUserProfile.ps1 -DeleteUnusedDay 1 -ExcludedUsers “marry” command in the Windows
PowerShell Console.

This command will delete of unused more than 10 days of user profile except the ‘marry’ user.
Here are some code snippets for your reference.
If($ProfileInfo -eq $null)
{
Write-Warning -Message "The item not found."
}
Else
{
Foreach($RemoveProfile in $ProfileInfo)
{
#Prompt message
$Caption = "Remove Profile"
$Message = "Are you sure you want to remove profile '$($RemoveProfile.LocalPath)'?"
$Choices = [System.Management.Automation.Host.ChoiceDescription[]]`
@("&Yes","&No")
[Int]$DefaultChoice = 1
$ChoiceRTN = $Host.UI.PromptForChoice($Caption,$Message,$Choices,$DefaultChoice)
Switch($ChoiceRTN)
{
0 {
Try{$RemoveProfile.Delete();Write-Host "Delete profile '$($RemoveProfile.LocalPath)' successfully."}
Catch{Write-Host "Delete profile failed." -ForegroundColor Red}
}
1 {break}
}
}
$ProfileInfo|Select-Object @{Expression={$_.__SERVER};Label="ComputerName"}, `
@{Expression={$_.ConvertToDateTime($_.LastUseTime)};Label="LastUseTime"},`
@{Name="Action";Expression={If(Test-Path -Path $_.LocalPath)
{"Not Deleted"}
Else
{"Deleted"}
}}
}
If($ProfileInfo -eq $null) { Write-Warning -Message "The item not found."} Else { Foreach($RemoveProfile in $ProfileInfo) { #Prompt message $Caption = "Remove Profile" $Message = "Are you sure you want to remove profile '$($RemoveProfile.LocalPath)'?" $Choices = [System.Management.Automation.Host.ChoiceDescription[]]` @("&Yes","&No") [Int]$DefaultChoice = 1 $ChoiceRTN = $Host.UI.PromptForChoice($Caption,$Message,$Choices,$DefaultChoice) Switch($ChoiceRTN) {0{ Try{$RemoveProfile.Delete();Write-Host "Delete profile '$($RemoveProfile.LocalPath)' successfully."} Catch{Write-Host "Delete profile failed." -ForegroundColor Red}}1{break}}} $ProfileInfo|Select-Object @{Expression={$_.__SERVER};Label="ComputerName"}, ` @{Expression={$_.ConvertToDateTime($_.LastUseTime)};Label="LastUseTime"},` @{Name="Action";Expression={If(Test-Path -Path $_.LocalPath) {"Not Deleted"} Else {"Deleted"}}}}
Windows PowerShell 2.0