Add-ADUserToGroup


posted by Richard Giles
10-06-2008

Downloads: 499
File size: 1.6kB
Views: 1,470

Embed
Add-ADUserToGroup
  1. ## ===================================================================== 
  2. ## Title       : Add-ADUserToGroup 
  3. ## Description : Add Active Directory User to a Group 
  4. ## Author      : Idera 
  5. ## Date        : 9/22/2008 
  6. ## Input       : -server  
  7. ##               -domain 
  8. ##               -ou 
  9. ##               -group 
  10. ##               -user 
  11. ##               -verbose  
  12. ##               -debug    
  13. ## Output      :  
  14. ## Usage       : PS> . Add-ADUserToGroup -server localhost:389 -domain Idera -ou sales -group Management -user Joe Smith -v -d 
  15. ## Notes       : Adapted from Windows PowerShell Cookbook, Lee Holmes 
  16. ## Tag         : PowerShell, AD 
  17. ## Change log  : 
  18. ## ===================================================================== 
  19.  
  20. param 
  21.    [string]$server = "localhost:389"
  22.    [string]$domain = "Idera"
  23.    [string]$ou = "sales"
  24.    [string]$group = "Outside Sales"
  25.    [string]$user = "Joe Smith"
  26.    [switch]$verbose
  27.    [switch]$debug 
  28.  
  29. function main() 
  30.     if ($verbose) {$VerbosePreference = "Continue"
  31.     if ($debug) {$DebugPreference = "Continue"
  32.     Add-ADUserToGroup $server $domain $ou $group $user 
  33.  
  34. function Add-ADUserToGroup($server,$domain,$ou,$group,$user
  35.     trap [Exception]  
  36.     
  37.         write-error $("TRAPPED: " + $_.Exception.Message); 
  38.         continue
  39.     
  40.  
  41.     # The group must be pre-existing for the specified OU and domain 
  42.     $groupObj = [adsi]"LDAP://$server/cn=$group,ou=$ou,dc=$domain,dc=COM" 
  43.      
  44.     # The user must be pre-existing for the specified OU and domain 
  45.     $userObj = "LDAP://$server/cn=$user,ou=$ou,dc=$domain,dc=COM" 
  46.      
  47.       $groupObj.Add($userObj
  48.  
  49. main 
Filed under: ,

Add an Active Directory user to an existing group

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.