Add or remove AD groups to or from Local groups


posted by Robert Burke
02-09-2011

Downloads: 759
File size: 1.1kB
Views: 4,606

Embed
Add or remove AD groups to or from Local groups
  1. <#  
  2. Name: remove-ad-group.ps1  
  3. Objective: To read a list of computernames from a file and then if the identified  
  4. group exists in the local administrators group remove it  
  5. Date: 01/31/2011  
  6. Email: rburke@rbconsulting.net 
  7. #>  
  8. #Name of user or group to be removed  
  9. $userName = 'IS_Tier3'  
  10. #Name of local group to remove user or group from  
  11. $localGroupName = 'Administrators'  
  12. #Variable that contains the contents of the servername list  
  13. $c = Get-Content "c:\Servers.txt"  
  14. #read each name and execute below script against each name in the list  
  15. foreach ($comp in $c)  
  16. {  
  17. #Variable reassignment  
  18. $computerName = $comp  
  19. #This reads the local domain and prepends it to the USERID  
  20. [string]$domainName = ([ADSI]'').name  
  21. # We are providing a yes so that we are not prompted for each computer  
  22. $confirm = "Y"  
  23. if ($confirm -eq "Y") {  
  24. ([ADSI]"WinNT://$computerName/$localGroupName,group").remove("WinNT://$domainName/$userName")  
  25. #We could output this to a file for future reference 
  26. Write-Host "User $domainName\$userName has been removed from local group $localGroupName on computer $computerName."  
  27. }  

Comments

itswithinmyreach wrote re: Add or remove AD groups to or from Local groups
on 12-10-2013 12:52 PM

Do you have a script that will remove a list of security groups from a list of servers?

I have 150k servers and 10 security groups that may or may not be in the local admins of each server and I wish to remove them.

Geferson Oliveira wrote re: Add or remove AD groups to or from Local groups
on 07-17-2014 11:04 AM

Do you have a script that will remove a list of security groups from a list of servers?

I have 150k servers and 10 security groups that may or may not be in the local admins of each server and I wish to remove them.

Copyright 2012 PowerShell.com. All rights reserved.