Solved

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

Posted on 2015-01-26
2
577 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 39

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

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Resolve DNS query failed errors for Exchange
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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 …

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now