?
Solved

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

Posted on 2015-01-26
2
Medium Priority
?
1,138 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
2 Comments
 
LVL 41

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

850 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