Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

export OU's of active directory users

Posted on 2014-09-28
5
Medium Priority
?
293 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
[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
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 2000 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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
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.
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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.

722 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