Solved

export OU's of active directory users

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

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

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.
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
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
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.
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