locked
2 OU and one Search Level RRS feed

  • Question

  • Hi all

    I am no good at scripting and need a little help, I have taken a look at the forums and cannot get what I need working.

    I have the following which is working fine.

    Get-ADUser -Filter * -SearchBase "OU=Test DC=Test ,DC=Test" -Properties SamAccountName, Physicaldeliveryofficename | Select SamAccountName, Physicaldeliveryofficename | Export-csv "C:\Office.csv"

    How do I add the following to this script

    Another OU, we have several OUs in our Domain and I need to search 2 of them and I need to keep the search scope to one level.

    Any help would be great.


    I.T support

    Wednesday, April 3, 2019 9:47 AM

All replies

  • Please format your code as code using the code posting tool provided on icon bar of the post editor (second to last icon). Thanks.

    You can use an array to store all OUs you like to query and a loop to iterate over this array

    $OUList = @(
        "OU=Test DC=Test ,DC=Test"
        "OU=Test2 DC=Test ,DC=Test"
        "OU=Test3 DC=Test ,DC=Test"
    )
    $Result = foreach ($OU in $OUList) {
        Get-ADUser -Filter * -SearchBase $OU -SearchScope OneLevel -Properties SamAccountName, Physicaldeliveryofficename | 
            Select-Object SamAccountName, Physicaldeliveryofficename
    }
    $Result | 
        Export-Csv "C:\Office.csv


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Wednesday, April 3, 2019 11:24 AM
    • Proposed as answer by ComputerScott Wednesday, April 3, 2019 8:12 PM
    Wednesday, April 3, 2019 10:23 AM
  • Hi Thank you that works a treat.

    Just one more thing how do I add the SearchScope to Onelevel which is the main part I cannot get working?


    I.T support

    Wednesday, April 3, 2019 10:54 AM
  • As always  - you should read the complete help for the cmdlets you're using - including the examples - to learn how to use it ...  Get-ADUser.

    You simply add -SearchScope OneLevel to your command line.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Wednesday, April 3, 2019 11:24 AM
    • Proposed as answer by ComputerScott Wednesday, April 3, 2019 8:12 PM
    Wednesday, April 3, 2019 11:23 AM