?
Solved

Powershell script help - import-csv return list of UPN and emailaddress

Posted on 2015-01-26
2
Medium Priority
?
921 Views
Last Modified: 2015-01-27
I have a CSV with one column with the header DisplayName.  I want to export a list of UPN's and EmailAddress's for each display name in that column.

I have tried the following:

Import-Module ActiveDirectory
$names = Import-Csv "D:\Users\bhart.difc\desktop\o365 stuff\book4.csv"
foreach ($name in $names) {
$user = Get-ADUser -filter { DisplayName -like $name.DisplayName }
$user.SamAccountName
}

And

$names = Import-Csv c:\temp\displaynames.txt
foreach ($name in $names) {
$user = Get-ADUser -filter { Name -like $name.DisplayName }
$user.SamAccountName
}

(*altering the column name when required)  

I get errors like:
ERROR: Get-ADUser : Property: 'DisplayName' not found in object of type: 'System.Management.Automation.PSCustomObject'.
get-aduser-displaynames.ps1 (12): ERROR: At Line: 12 char: 9
ERROR: + $user = Get-ADUser -filter { DisplayName -like $name.DisplayName }
ERROR: +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
ERROR:     + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser


This is on a Win8.1 client machine, against 2k8 and 2012 DC's.  Maybe I've just used the wrong keywords googling.  Someone here possibly knows of a simpler terminology to accomplish this.

Thanks
0
Comment
Question by:Ben Hart
[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 Comments
 
LVL 40

Assisted Solution

by:footech
footech earned 1000 total points
ID: 40571408
You're the second one today to post on the same issue.  :)
The problem is with how PS is able to evaluate variables inside of the filter block for Get-ADUser.  The most straight-forward fix is below.
foreach ($name in $names) {
 $display = $name.DisplayName
$user = Get-ADUser -filter { DisplayName -like $display }
 $user.SamAccountName
 }

Open in new window


See this thread for some more info.
http:Q_28514109.html
0
 
LVL 53

Accepted Solution

by:
Will Szymkowski earned 1000 total points
ID: 40572965
You can also use the following script to accomplish this as well.

The command below will out-put the data on the screen
Import-Module ActiveDirectory
$names = Import-Csv "D:\Users\bhart.difc\desktop\o365 stuff\book4.csv"
$names = foreach ($name in $names) {
$name.DisplayName
Get-ADUser -Identity $name.DisplayName | select UserPrincipalName, Mail
}

Open in new window


The command below will export the info to a csv
Import-Module ActiveDirectory
$names = Import-Csv "D:\Users\bhart.difc\desktop\o365 stuff\book4.csv"
$names = foreach ($name in $names) {
$name.DisplayName
Get-ADUser -Identity $name.DisplayName | select UserPrincipalName, Mail | out-file "c:\UserExport.csv" -append
}

Open in new window


Will.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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…
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 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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

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