Monday, April 6, 2015

Powershell: Modifying ADGroup membership

Here's a script I made today that is destined for users who manage certain AD distribution groups for their own departments and the like.





function list_groups
    {
    Get-ADGroup -Filter "managedby -eq '$($user.DistinguishedName)'" |fl samaccountname
    }
function add_member
    {
    $newuser = read-host = "Enter username to add"
    $group = read-host = "Enter the group name you wish to modify as they are named above"
   add-adgroupmember -identity $group -members $newuser
   get-adgroupmember -identity $group |fl name
    }
function remove_member
    {
    $olduser = read-host = "Enter the username you wish to remove"
    $group1 = read-host = "Enter the group name as they are named above"
   remove-adgroupmember -identity $group1 -members $olduser -confirm:$false
   get-adgroupmember -identity $group1 |fl name
    }

$username = Read-host "Enter your username"
$user = Get-ADUser $username

[int]$xMenuChoiceA = 0
do {

Write-host "1. List groups I manage"  -fore Cyan
Write-host "2. Add members to a group" -fore Cyan
Write-host "3. Delete members from a group" -fore Cyan
Write-host "4. Quit and exit" -fore Cyan

$xMenuChoiceA = read-host "Please enter an option 1 to 4"

Switch( $xMenuChoiceA ){
  1
    {
    list_groups
    }
  2
    {
    add_member
    }
 
  3
    {
    remove_member
    }
default
    {
    write-host "Valid responses are 1,2,3,4"
    }
    }
}while  ( $xMenuChoiceA -le 3 )

No comments:

Post a Comment