删除用户个人资料 $ UserFolderPath1 =“\ $ Server \ C $ \ Users \ $ UserName”
Try {
(Get-WmiObject -ComputerName $Server Win32_UserProfile | Where-Object {$_.LocalPath -eq "C:\Users\$UserName"}).Delete()
Write-Host -ForegroundColor Green "$UserName has been deleted from $Server"
} Catch [System.Management.Automation.MethodInvocationException]{
Write-Host -ForegroundColor Red "ERROR: Profile is currently locked on $Server "
} Catch [System.Management.Automation.RuntimeException] {
Write-Host -ForegroundColor Yellow -BackgroundColor Blue "INFO: $UserName Profile does not exist on $host"
} Catch {
Write-Host -ForegroundColor Red "ERROR: an unknown error occoured. The error response was $($error[0])"
}
用户文件夹已删除,但测试路径c:\ users \ userprofilefolder仍显示为true
if(test-path "$UserFolderPath1"){
Write-host "user folder found"
Get-ChildItem $UserFolderPath1 -Recurse | Remove-Item -recurse -Force
Remove-Item $UserFolderPath1 -Force
}