Solved

Powershell script export users properties but exclude certain OU

Posted on 2013-10-23
7
2,906 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 22

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 40

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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 40

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A hard and fast method for reducing Active Directory Administrators members.
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 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