Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Deleting Old User Profiles

$
0
0

I would like to delete any user profiles older than 30 days prior to backing up a client system. I am getting an access denied when trying to remove the c:\users\jsmith\AppData\Local\Application Data' directory. Also is there any way to delete all the registry info associated with that user?

I have to use powershell v2

Here is what i have so far … thanks

[cmdletbinding(SupportsShouldProcess)]
 
Param(
[Parameter(Position=0)]
[ValidateNotNullorEmpty()]
[int]$Days= 30
)
 
 
 
Write-Warning "Filtering for user profiles older than $Days days" 
 
$profile = gwmi  win32_userprofile  | ? {$_.localpath -like "*c:\users\*" -and  $_.localpath -notlike "C:\Users\Administrator"}  | 
select  localpath,@{ Name = "lastUseTime" ; Expression = { $_.Converttodatetime( $_.LastuseTime ) } } | 
Where {$_.LastUseTime -lt $(Get-Date).AddDays(-$days)} 
 
if ($profile -eq $null){Write-Host  "No user profiles older than $Days days where found " 
}
else {
remove-item $profile.localpath -Recurse -Force 
}

Viewing all articles
Browse latest Browse all 13067

Trending Articles