Solved

export OU's of active directory users

Posted on 2014-09-28
5
270 Views
Last Modified: 2014-10-16
Hi,

I would like to read in from a text / CSV file a list of AD users and export the OU's that they reside in to a text / csv file

thanks
0
Comment
Question by:cmatchett
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40348817
Try this:
$users = import-csv c:\userlist.csv
foreach ($user in $users)
{
Get-ADUser -Identity $user -Properties DistinguishedName | export-csv c:\usersWithOU.csv -append
}

Open in new window


Or try this:
Try this:
$users = import-csv c:\userlist.csv
foreach ($user in $users)
{
$userinfo = Get-ADUser -Identity $user
($userinfo.DistinguishedName -split “,”, 2)[1] | export-csv c:\usersWithOU.csv -append
}

Open in new window

0
 

Author Comment

by:cmatchett
ID: 40348924
i'm getting the following...


Get-ADUser : Cannot bind parameter 'Identity'. Cannot convert value "@{users=cmatchett}" to type
"Microsoft.ActiveDirectory.Management.ADUser". Error: "Cannot convert the "@{users=cmatchett}" value of type
"System.Management.Automation.PSCustomObject" to type "Microsoft.ActiveDirectory.Management.ADUser"."
At C:\scripts\cm\Get-ADOU.ps1:4 char:34
+ $userinfo = Get-ADUser -Identity $user
0
 

Author Comment

by:cmatchett
ID: 40348926
i get the error with both scripts
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40349444
make sure the source csv file only contains username
otherwise
try replacing the following line :
$userinfo = Get-ADUser -Identity $user
with
$userinfo = Get-ADUser -Identity $user.users
0
 
LVL 17

Accepted Solution

by:
Learnctx earned 500 total points
ID: 40349452
When I'm reporting something like OU's I'm a big fan of using the canonical name. It reports much nicer. Might not be to everyone's taste but it reads nice to non-technical folk.

$results = @()
$users = import-csv c:\userlist.csv
foreach ($user in $users)
{
   $Results += Get-ADUser -Identity $user.username -Properties canonicalname | select Name, @{n="OU";e={$_.CanonicalName-replace "\/$($_.name)"}} 
}
$Results | Export-CSV -NoType c:\results.csv

Open in new window


Append I think for Export-CSV is only available from PowerShell 4 onward. If you're running Windows 8/2012 it will be there, or if you've upgraded to Powershell 4, otherwise use an array to store the results.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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 …

785 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