Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-01-26
2
Medium Priority
?
1,048 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 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
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 …
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

610 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