40 lines
2.0 KiB
PowerShell
40 lines
2.0 KiB
PowerShell
# Define the username of the provisioning admin account
|
|
$ProvisioningAdminUsername = "ek-provadmin"
|
|
|
|
# Exit if this is a domain controller
|
|
If ( $(Get-CimInstance -ClassName Win32_OperatingSystem -Property ProductType | Select-Object -ExpandProperty ProductType) -eq "2" ) {
|
|
Write-Output "Cannot create or modify local accounts on a domain controller"
|
|
Exit 0
|
|
}
|
|
|
|
# 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
|
|
$SID = $ProvisioningAdmin.SID.Value
|
|
Write-Output "Removing provisioning admin account`nUsername: ${ProvisioningAdminUsername}`nSID: $SID"
|
|
Try { Remove-LocalUser -SID $SID -ErrorAction Stop }
|
|
Catch { Write-Error "The provisioning admin account could not be removed`n"+$_.Exception.Message ; Exit 1 }
|
|
|
|
# Fully remove the provisioning admin profile
|
|
Write-Output "Removing provisioning admin CIM instance"
|
|
Try { Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.SID -eq $SID } -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 provisioning admin profile directory: ${ProfilePath}"
|
|
Try { Remove-Item $ProfilePath -Recurse -Force }
|
|
Catch { Write-Error "The provisioning admin profile directory could not be removed`n"+$_.Exception.Message ; Exit 1 }
|
|
}
|