Solved

export OU's of active directory users

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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