Powershell script export users properties but exclude certain OU

Hi All

I need to export all users home directory , we have a top level Accounts OU with about 15 OU's below it, I need to query all except two of them (as they contain Service account and admin accounts) The query below is giving me what I need but I need to add the additional code to exclude the two OUs,
OU  = Service Accounts
OU = Admin Accounts

Get-ADUser -searchbase "OU=accounts,OU=UK,OU=Company,DC=Domain,DC=com" -Filter * -Property * | Select-Object -Property sAMAccountName,homeDirectory | Export-CSV -Path C:\temp\users4.csv 

Open in new window

Could anyone please help me with this

Many Thanks
Who is Participating?
You can filter based on the DistinguishedName..
For example..
Get-ADUser -searchbase "OU=accounts,OU=UK,OU=Company,DC=Domain,DC=com" -Filter * -Property * | 
?{$_.DistinguishedName -notmatch 'OU=SharedMailboxes,OU=accounts,OU=UK,OU=Company,DC=Domain,DC=com' `
    -and $_.DistinguishedName -notmatch 'OU=ServiceAccounts,OU=accounts,OU=UK,OU=Company,DC=Domain,DC=com'} | 
Select-Object -Property sAMAccountName,homeDirectory | Export-CSV -Path C:\temp\users4.csv 

Open in new window

Check this question..
Joseph MoodyBlogger and wearer of all hats.Commented:
You could do something like:

Get-ADUser -searchbase "OU=accounts,OU=UK,OU=Company,DC=Domain,DC=com" -Filter * -Property * | Where-Object {$_.ParentContainer -ne "OU=Service Accounts,OU=accounts,OU=UK,OU=Company,DC=Domain,DC=com" -OR $_.ParentContainer -ne "OU=Admin Accounts,OU=accounts,OU=UK,OU=Company,DC=Domain,DC=com" | REST OF CODE

Open in new window

You might have to play around with a bit.
One way to do it would be as below.
Get-ADUser -searchbase "OU=accounts,OU=UK,OU=Company,DC=Domain,DC=com" -Filter * -Property * |
 Where { $_.DistinguishedName -notmatch "OU=Service Accounts" -or $_.DistinguishedName -notmatch "OU=Admin Accounts"} |
 Select-Object -Property sAMAccountName,homeDirectory |
 Export-CSV -Path C:\temp\users4.csv

Open in new window

Another would be to first get a list of all the OUs, filtering out the ones you don't want, and then search in each one individually.
By the way, if you're only interested in the sAMAccountName and homeDirectory attributes, I wouldn't use -properties * for the Get-ADUser command, but instead limit it to just the attributes you want, it will be much more efficient.
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Raj-GTSystems EngineerCommented:
There is another, much simpler (IMHO) way. Since you are exporting the data to CSV, why not just add the CanonicalName property to the select-object part and sort and delete stuff in Excel.

Get-ADUser -searchbase "OU=accounts,OU=UK,OU=Company,DC=Domain,DC=com" -Filter * -Property * | Select-Object -Property sAMAccountName,CanonicalName,homeDirectory | Export-CSV -Path C:\temp\users4.csv 

Open in new window

I guess -or condition won’t work.. it should be -and..
@Subsun - you're right.  :)
ncomperAuthor Commented:
Thanks All
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.