Remove domain accounts from local Administrators group


Does anyone have a script that removes domain accounts from the local administrators group on machines ?

So for instance if I have a list of machines and a list of domain accounts that I need removed from the local administrators
group on those machines .

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
You will not be able to accomplish this. When you add a computer to the domain 2 things happen. Domain Admins Group is automatically added to the Local Administrators Group on all Domain Computers.  Also Domain Users is also added to the Local Users group as well.

So you cannot remove domain admins from local admins. You can remove the users from Domain Admins group which will then remove the access to local admins but you cannot remove Domain Admins completely.

So if i am reading your question correctly, what your asking for cannot be accomplished.

Try this:
$InputFile = "C:\Temp\test.txt"
$OutputFile = "C:\Temp\test.csv"
Import-Csv -Path $InputFile -Header Machine,User |
	% {
		"$($_.Machine): $($_.User) ... " | Write-Host -ForegroundColor White -NoNewline
		$Result = $_ | Select-Object -Property *, Result
		$ResultColor = "Green"
		If (Test-Connection -Count 2 -ComputerName $_.Machine -Quiet) {
			Try {
				$DomainName, $UserName = $_.User.Split("\")
				([ADSI]"WinNT://$($_.Machine)/Administrators,group").Remove("WinNT://$($DomainName)/$($UserName)") | Out-Null
				$Result.Result = "OK: removed"
			} Catch {
				If ($_.Exception.InnerException.HResult -eq -2147023519) {
					$Result.Result = "OK: no member"
				} Else {
					$Result.Result = "0x{0:X8}: $($_.Exception.InnerException.Message.Trim())" -f $_.Exception.InnerException.HResult
					$ResultColor = "Red"
		} Else {
			$Result.Result = "Offline"
			$ResultColor = "Red"
		$Result.Result | Write-Host -ForegroundColor $ResultColor
		$Result | Write-Output
	} |
	Export-Csv -Path $OutputFile -NoTypeInformation

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
Hi oBdA .. wow ! this so helped me today !! thanks a ton !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.