Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. <#
  2. I broke out the Get-CIMInstance and Get-WMIObject types for a couple of reasons. 1. Because I kept running into issues on 2008 R2
  3. where CIM wasn't working due to the fact they aren't configured for remoting out of the box like 2012 servers are. 2. In order to
  4. train system admins on how to use both versions in case one or the other doesn't work for them. Here, I break down the two based
  5. on the version of the OS but you can elect to use one or the other depening on your environment. In CIM you can specify the protocol
  6. type which could help eliminate the need for the WMI version but the WMI version will be supported on servers with older versions
  7. of PowerShell.
  8. #>
  9. #Specifying the EAP
  10. $ErrorActionPreference = "Stop"
  11. #Create a decimal based OS version since CIMInstance doesn't work below version 6.2
  12. $OSVersion = [decimal]([environment]::OSVersion.Version.Major,[environment]::OSVersion.Version.Minor -join ".")
  13. #The name of the user's profile folder as it exists on the session host
  14. $ProfileNames = @("username.DOMAIN","username1.DOMAIN")
  15. #Array of session host servers
  16. $Servers = @("RDSH01","RDSH02","RDSH03","RDSH04","RDSH05")
  17. ""
  18. $Servers |
  19. %{
  20. try
  21. {
  22. foreach($P in $ProfileName)
  23. {
  24. Write-Host "Removing the " -NoNewLine
  25. Write-Host $P -NoNewline -ForegroundColor Yellow
  26. Write-host " profile from " -NoNewline
  27. Write-Host $_ -ForegroundColor Cyan
  28. #Machines 2012 and newer use CIM
  29. $Filter = "localpath='C:\\Users\\$P'"
  30. if($OSVersion -ge 6.2)
  31. {
  32. $X = Get-CimInstance Win32_UserProfile -Computer $_ -filter "$Filter" |
  33. Remove-CimInstance
  34. }
  35. else
  36. {
  37. $X = Get-WmiObject Win32_UserProfile -Computer $_ -filter "$Filter"
  38. $X.Delete()
  39. }
  40. Write-Host "- Successfully removed" -ForegroundColor Green
  41. }
  42. }
  43. catch
  44. {
  45. Write-Host "- $($_.Exception.Message)" -ForegroundColor Red
  46. }
  47. }
  48. ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement