Solved

Powershell script export users properties but exclude certain OU

Posted on 2013-10-23
7
3,007 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
What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

 
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

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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 tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
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 …
Suggested Courses

623 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