Solved

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

Posted on 2015-01-26
2
763 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 40

Assisted Solution

by:footech
footech earned 250 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 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In-place Upgrading Dirsync to Azure AD Connect
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

685 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