add script to remove provisioning admin user and associated profile
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
# Define the username of the provisioning admin account
|
||||
$ProvisioningAdminUsername = "ek-provisioningadmin"
|
||||
|
||||
# Get the provisioning admin user account
|
||||
$ProvisioningAdmin = Get-LocalUser -Name $ProvisioningAdminUsername -ErrorAction SilentlyContinue
|
||||
|
||||
# Exit if the provisioning admin account does not exist
|
||||
If ( $ProvisioningAdmin -eq $false ) { Write-Output "Provisioning Admin account does not exist" ; Exit 0 }
|
||||
|
||||
# Exit if the user is logged in
|
||||
If ( $(Get-CimInstance -Class Win32_Process -Filter 'name = "explorer.exe"' | Invoke-CimMethod -MethodName getowner).User -contains $ProvisioningAdminUsername ) {
|
||||
Write-Output "User is logged in, cannot complete removal of provisioning admin account"
|
||||
Exit 1
|
||||
}
|
||||
|
||||
# Remove the provisioning admin account
|
||||
Try { Remove-LocalUser -SID $ProvisioningAdmin.SID.Value -ErrorAction Stop }
|
||||
Catch { Write-Error "The provisioning admin account could not be removed`n"+$_.Exception.Message ; Exit 1 }
|
||||
|
||||
# Fully remove the provisioning admin profile
|
||||
Try { Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.SID -eq $ProvisioningAdmin.SID.Value } -ErrorAction SilentlyContinue | Remove-CimInstance }
|
||||
Catch { Write-Error "The provisioning admin profile could not be removed`n"+$_.Exception.Message ; Exit 1 }
|
||||
|
||||
# Delete any remaining provisioning admin directories
|
||||
$ProfilePath = "${Env:SystemDrive}\Users\$ProvisioningAdminUsername"
|
||||
If ( Test-Path $ProfilePath ) {
|
||||
Write-Output "Deleting ""$ProfilePath"""
|
||||
Remove-Item $ProfilePath -Recurse -Force
|
||||
}
|
||||
Reference in New Issue
Block a user