none
Powershell - Excluding multiple OUs from a Get-ADUser Script RRS feed

  • Question

  • Hi all,

    I am trying to run a Powershell script to export users and some fields from AD to a CSV.

    The code I have started is below.

    However I am having issues. The output is empty for some reason and I cant work it out.

    import-module ActiveDirectory
    
    Get-ADUser -filter * -searchbase "OU=USERS,OU=Groups,DC=DC1,DC=DC2,DC=DC3,DC=DC4" -properties * |
    
    ? {
        $_.distinguishedname -notlike '*OU=ArchiveManager,OU=USERS,OU=Groups,DC=DC1,DC=DC2,DC=DC3,DC=DC4' -and
        $_.distinguishedname -notlike '*OU=CIGE - Non Roaming,OU=USERS,OU=Groups,DC=DC1,DC=DC2,DC=DC3,DC=DC4' -and
        $_.distinguishedname -notlike '*OU=External Addresses,OU=USERS,OU=Groups,DC=DC1,DC=DC2,DC=DC3,DC=DC4' -and
        $_.distinguishedname -notlike '*OU=F5 Non Council Employees,OU=USERS,OU=Groups,DC=DC1,DC=DC2,DC=DC3,DC=DC4' -and
        $_.distinguishedname -notlike '*OU=Mailbox Only,OU=USERS,OU=Groups,DC=DC1,DC=DC2,DC=DC3,DC=DC4' -and
        $_.distinguishedname -notlike '*OU=Security User Only,OU=USERS,OU=Groups,DC=DC1,DC=DC2,DC=DC3,DC=DC4' -and
        $_.distinguishedname -notlike '*OU=Server Users,OU=USERS,OU=Groups,DC=DC1,DC=DC2,DC=DC3,DC=DC4' -and
        $_.distinguishedname -notlike '*OU=Special Users - Non Roaming,OU=USERS,OU=Groups,DC=DC1,DC=DC2,DC=DC3,DC=DC4' -and
        $_.distinguishedname -notlike '*OU=Vendor Accounts,OU=USERS,OU=Groups,DC=DC1,DC=DC2,DC=DC3,DC=DC4'  
        
        }
    
    select EmailAddress, givenName, sn, department, physicalDeliveryOfficeName, mobile |
    
    export-csv "c:\scripts\Export.csv" -NoTypeInformation
    
    

    Thanks in advance

    Brett


    • Moved by Bill_Stewart Wednesday, September 4, 2019 7:41 PM This is not "fix/debug/rewrite script for me" forum
    Wednesday, March 27, 2019 11:26 PM

All replies

  • The only DN you will get is the one in SearchBase.  Be sure you are return users.

    Your "select" is not selecting from anything.  You are missing a pipe.


    \_(ツ)_/

    Wednesday, March 27, 2019 11:58 PM
  • Thanks jrv.

    Do I need to move my existing select statement?

    Where do I need to put the missing pipe?

    Thursday, March 28, 2019 12:07 AM
  • Hi 

    Please share the error or

    Post your script requirement in script repository of microsoft.We will work on the script based on your requirement and share it to you.

    Thursday, March 28, 2019 12:08 AM
  • Hi 

    Please share the error or

    Post your script requirement in script repository of microsoft.We will work on the script based on your requirement and share it to you.


    What?  What are you trying to say?  The user asked a simple question.  It is not a request for a script.

    \_(ツ)_/

    Thursday, March 28, 2019 1:40 AM
  • Thanks jrv.

    Do I need to move my existing select statement?

    Where do I need to put the missing pipe?

    You have to pipe the results from the first part to the select statement.  A select statement all by itself does nothing.

    I recommend that you should learn to use help before you try to write a script.

    help select-object -online


    \_(ツ)_/

    Thursday, March 28, 2019 1:46 AM