0

PowerShell - Add and Remove Users from Active Directory Group

by Jim 28. November 2011 13:17

I had a need to routinely update the members of a security group in Active Directory.  The users in an OU were to be added/removed from the security group on a scheduled basis.

  1. Import-Module ActiveDirectory
  2. Get-ADGroupMember -Identity "Some Security Group" | ForEach-Object { $Member = $_.SamAccountName; Remove-AdGroupMember -Identity "Some Security Group" -Members $Member -Confirm:$false }
  3. Get-User -OrganizationalUnit "Some OU" | ForEach-Object { Add-ADGroupMember -Identity "Some Security Group" -Members $_.SamAccountName }

This could be written into a powershell script file to be run on a schedule.

Tags: ,

PowerShell

Powered by BlogEngine.NET 2.5.0.6
Original Design by Laptop Geek, Adapted by onesoft