Solved

export OU's of active directory users

Posted on 2014-09-28
5
263 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 18

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 18

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 16

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

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now