Solved

Powershell script export users properties but exclude certain OU

Posted on 2013-10-23
7
2,652 Views
Last Modified: 2013-10-24
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
0
Comment
Question by:ncomper
7 Comments
 
LVL 21

Expert Comment

by:Joseph Moody
ID: 39594680
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.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 300 total points
ID: 39594695
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..
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28009174.html
1
 
LVL 39

Expert Comment

by:footech
ID: 39594703
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Assisted Solution

by:Raj-GT
Raj-GT earned 200 total points
ID: 39594708
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

0
 
LVL 40

Expert Comment

by:Subsun
ID: 39594718
I guess -or condition won’t work.. it should be -and..
0
 
LVL 39

Expert Comment

by:footech
ID: 39594748
@Subsun - you're right.  :)
0
 
LVL 5

Author Closing Comment

by:ncomper
ID: 39597306
Thanks All
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now