?
Solved

Powershell script export users properties but exclude certain OU

Posted on 2013-10-23
7
Medium Priority
?
3,184 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 1200 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 41

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Assisted Solution

by:Raj-GT
Raj-GT earned 800 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 41

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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
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 …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

718 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