Solved

PS Script that will list Name and OU of a user reading from a CSV of SamAccountnames.

Posted on 2013-12-19
6
624 Views
Last Modified: 2013-12-23
I need a PS script that will read a CSV of SamAccountNames and list out the Users name and the OU they are in. Output should be a CSV. I.e.
 SamAccountname, Student Jones, Ou

the OUs are name simular to this.

Student-Male/Smith/11th/Users/s300/ourcity.countyedu.net

I have seen several PS scripts that will give the AD User properties but not sure how to modify them to read a CSV and then give the Full name and OU.

Thanks
0
Comment
Question by:Risrollout
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
Recept earned 500 total points
ID: 39730815
The native Powershell way (requires a 2008+ Domain Controller). The OU output can be cleaned up if needed.
Import-Csv .\input.csv | ForEach-Object {Get-ADUser -Identity $_.SamAccountName -Properties * | Select-Object SamAccountName, Name, @{name="OU";expression={($_.DistinguishedName -split ",",2)[1]}}} | Export-Csv .\output.csv

Open in new window


Or the Quest AD Cmdlets way (requires Quest AD Cmdlets)
Import-Csv .\input.csv | ForEach-Object {Get-QADUser -Identity $_.SamAccountName | Select-Object SamAccountName, Name, @{name="OU";expression={$_.ParentContainerDN}}} | Export-Csv .\output.csv

Open in new window


The format of the .csv file should be
SamAccountName,
user1,
user2,

Open in new window

0
 
LVL 37

Expert Comment

by:Mahesh
ID: 39731150
Just try Bulk AD Users tool from Wisesoft

Within tool you can practically generate csv reports for any user properties (Through properties to Load option) in bulk and even can modify properties in bulk through csv update feature

http://www.wisesoft.co.uk/software/bulkadusers/default.aspx

Tool is freeware and can be registered as free

Mahesh
0
 

Author Comment

by:Risrollout
ID: 39732434
Thanks for the answers.
It looks like I did not give a good explanation of what we are trying to do with this. We are trying to pull information out of AD not add it.
 We have a software package that gives us a report that shows the login name of a person who logs into a PC and uses a piece of software( I.e. PhotoShop). That software does not have an AD connection nor do we want it to. We will take that report and pull out the login names (SamAccountname) and use that to pull the users Full Name and what OU they are in for another report. (could be several hundred names. The OUs tell us the School, HR teacher, Grade and so forth.)
So what we have is a CSV that looks like this.

S2013JoeB001,
S2013CheC001,

And looking for a Output CSV like this; SamAccountName, Full Name, OU.

S2013JoeB001,Joe Bass,Student-Male/Smith/11th/Users/s300/ourcity.countyedu.net.

PeterRecz,
When I try to run your line (we are running Windows 2008 R2 and have the AD Module for Win PS turned on) I get the output file created but no content. I do not quite understand how you have the input CSV structured

Hope this helps
.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 37

Expert Comment

by:Mahesh
ID: 39734900
The above tool will give you output as u wanted
only OU output will be in LDAP format

There is option called "Properties to load" in software which will allow you to select any user property and also u can export the same in csv

You can pull users from entire domain, OU or from list of users if you have already

Mahesh
0
 
LVL 5

Expert Comment

by:Recept
ID: 39735099
Hi Risrollout,

Just to clarify the Powershell scripts will pull information out of AD, it won't add to it.

I've now attached an example csv file.
example.csv
0
 

Author Comment

by:Risrollout
ID: 39736826
PeterRecz,
The PS does not like the "{Get-ADUser -Identity" section. I get the error message

 "Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try
 the command again.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
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