Get-ADUsersInOU


posted by Richard Giles
10-06-2008

Downloads: 515
File size: 1.3kB
Views: 1,704

Embed
Get-ADUsersInOU
  1. ## ===================================================================== 
  2. ## Title       : Get-ADUsersInOU 
  3. ## Description : Get Active Directory Users In OU 
  4. ## Author      : Idera 
  5. ## Date        : 9/22/2008 
  6. ## Input       : -server  
  7. ##               -domain 
  8. ##               -ou 
  9. ##               -verbose  
  10. ##               -debug    
  11. ## Output      :  
  12. ## Usage       : PS> . Get-ADUsersInOU -server localhost:389 -domain Idera -ou sales -v -d 
  13. ## Notes       : Adapted from Windows PowerShell Cookbook, Lee Holmes 
  14. ## Tag         : PowerShell, AD 
  15. ## Change log  : 
  16. ## ===================================================================== 
  17.  
  18. param 
  19.    [string]$server = "localhost:389"
  20.    [string]$domain = "Idera"
  21.    [string]$ou = "sales"
  22.    [switch]$verbose
  23.    [switch]$debug 
  24.  
  25. function main() 
  26.     if ($verbose) {$VerbosePreference = "Continue"
  27.     if ($debug) {$DebugPreference = "Continue"
  28.     Get-ADUsersInOU $server $domain $ou 
  29.  
  30. function Get-ADUsersInOU($server,$domain,$ou
  31.     trap [Exception]  
  32.     
  33.         write-error $("TRAPPED: " + $_.Exception.Message); 
  34.         continue
  35.     
  36.  
  37.     $ouObj = [adsi]"LDAP://$server/ou=$ou,dc=$domain,dc=COM" 
  38.     $searcher = New-Object System.DirectoryServices.DirectorySearcher $ouObj 
  39.     $searcher.Filter = '(objectClass=User)' 
  40.     $searcher.FindOne() 
  41.  
  42. main 
Filed under: , ,

Get users in an Active Directory Organizational Unit

Copyright 2012 PowerShell.com. All rights reserved.