locked
Get-Aduser Effective Multi-OU Search RRS feed

  • Question

  • Hello,

    Referring to the get-aduser –searchbase parameter, is this just a single value parameter or is it usable for multi-OU searches for example:

    Get-aduser –filter * -searchbase “OU=MyOU,DC=domain,DC=com” OU#2, OU#3 -property WhenCreated,Name | FT | out-file dir


    Friday, September 6, 2013 7:52 PM

Answers

All replies

  • Hello,

    Referring to the get-aduser –searchbase parameter, is this just a single value parameter or is it usable for multi-OU searches for example:

    Get-aduser –filter * -searchbase “OU=MyOU,DC=domain,DC=com” OU#2, OU#3 -property WhenCreated,Name | FT | out-file dir


    When in doubt always refer to the source:
    http://technet.microsoft.com/en-us/library/ee617241.aspx

    You'll find the datatype expected by the parameter is String, not an array.  Your code above should error out right away.

    In this case you'll need to make three calls to Get-ADUser and specify the different OUs each time.
    Friday, September 6, 2013 7:56 PM
  • Yes the errors made me wonder if I could use multiple strings on that, I’ll look into another method, thanks.


    • Proposed as answer by m.afzaal Wednesday, January 16, 2019 5:39 AM
    • Unproposed as answer by jrvModerator Wednesday, January 16, 2019 5:56 AM
    Friday, September 6, 2013 8:00 PM
  • If you have an array of strings already made, you can just pipe it though a loop:

    $ous = 'OU=Test 1,DC=domain,DC=com','OU=Test 2,DC=domain,DC=com'
    
    $ous | ForEach { Get-ADUser -Filter * -SearchBase $_ }


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,610+ strong and growing)



    Friday, September 6, 2013 8:45 PM
    Moderator
  • $UsersInOU1 = Get-aduser filter * -searchbase OU=MyOU,DC=domain,DC=com -property WhenCreated,Name | FT | out-file dir

    $UsersInOU2 = Get-aduser filter * -searchbase OU=OU#2,DC=domain,DC=com -property WhenCreated,Name | FT | out-file dir

    $UsersInOU3 = Get-aduser filter * -searchbase OU=OU#3,DC=domain,DC=com -property WhenCreated,Name | FT | out-file dir

    $AllUsersList = $UsersInOU1 + $UsersInOU2 + $UsersInOU3

    $AllUsersList | FT Name,WhenCreated

    Tuesday, September 8, 2015 12:09 PM
  • You realize this thread is over two years old and already answered, right?


    Tuesday, September 8, 2015 12:12 PM
    Moderator
  • You could also do a PSObject based on some of the stuff already mentioned, and easily pipe $results to a CSV etc:

    $ous = 'OU=Users,DC=domain,DC=com','OU=External Users,DC=domain,DC=com'
    $results = @()
    foreach ($ou in $ous){
    foreach ($user in get-aduser -filter {enabled -eq $true} -searchbase $ou  -properties * ){
    $obj = New-Object -TypeName PSObject
    $obj | Add-Member -MemberType NoteProperty -Name SamAccountName -Value $user.samaccountname
    $obj | Add-Member -MemberType NoteProperty -Name CN -Value $user.canonicalname
    $obj | Add-Member -MemberType NoteProperty -Name CN -Value $user.mail
    $obj | Add-Member -MemberType NoteProperty -Name CN -Value $user.userprincipalname
    $obj | Add-Member -MemberType NoteProperty -Name CN -Value $user.givenname
    $obj | Add-Member -MemberType NoteProperty -Name CN -Value $user.surname
    $results += $obj
    }
    }
    $results | Out-GridView
    


    Wednesday, July 11, 2018 5:03 AM
  • You could also do a PSObject based on some of the stuff already mentioned, and easily pipe $results to a CSV etc:

    $ous = 'OU=Users,DC=domain,DC=com','OU=External Users,DC=domain,DC=com'
    $results = @()
    foreach ($ou in $ous){
    foreach ($user in get-aduser -filter {enabled -eq $true} -searchbase $ou  -properties * ){
    $obj = New-Object -TypeName PSObject
    $obj | Add-Member -MemberType NoteProperty -Name SamAccountName -Value $user.samaccountname
    $obj | Add-Member -MemberType NoteProperty -Name CN -Value $user.canonicalname
    $obj | Add-Member -MemberType NoteProperty -Name CN -Value $user.mail
    $obj | Add-Member -MemberType NoteProperty -Name CN -Value $user.userprincipalname
    $obj | Add-Member -MemberType NoteProperty -Name CN -Value $user.givenname
    $obj | Add-Member -MemberType NoteProperty -Name CN -Value $user.surname
    $results += $obj
    }
    }
    $results | Out-GridVie


    A little more PS experience would show you why your code doesn't work and also  show you why most of it is unnecessary.

    Try this. It is how PowerShell is designed to work with objects and with the pipeline:

    $ous = 'OU=Users,DC=domain,DC=com','OU=External Users,DC=domain,DC=com'
    $ous |
        ForEach-Object{
            Get-AdUser -filter {enabled -eq $true} -searchbase $_  -properties canonicalname,mail,userprincipalname
        } |
        Select SamAccountName,canonicalname,mail,userprincipalname,givenname,surname |
        Out-GridView


    Note that you cannot have multiple properties with the same name, Typo maybe?


    \_(ツ)_/


    Wednesday, July 11, 2018 7:04 AM
    Moderator