locked
Cant make work with variable in Get-ADuser command to get UPN RRS feed

  • Question

  • I cant seem to figure this out, any help would be appreciated.

    $users=Import-Csv-PathC:\Scripts\UPNUserlookup.csv

    ForEach

    ($userin$users){Get-ADUser-identity$user|Select-ObjectName,UserPrincipalName}


    Monday, February 25, 2019 5:54 PM

Answers

  • What is the user called in the csv?  I'm assuming the heading is "user".

    $users=Import-Csv -Path C:\Scripts\UPNUserlookup.csv
    ForEach
    ($user in $users){Get-ADUser -identity $user.user|
      Select-Object Name,UserPrincipalName}

    • Edited by JS2010 Tuesday, February 26, 2019 9:46 PM
    • Marked as answer by BucketheadFan Thursday, February 28, 2019 6:18 PM
    Monday, February 25, 2019 8:38 PM
  • This is one way.

    $Results = @()
    $users = Import-Csv -Path C:\Scripts\UPNUserlookup.csv
    
    ForEach ($user in $users)
    {
        $ADUser = Get-ADUser -identity $user.user | Select-Object Name, UserPrincipalName
        $Results += New-Object PsObject -Property @{
            Name = $ADUser.Name
            UserPrincipalName = $ADUser.UserPrincipalName
        }
    }
    $Results | Export-CSV -Path "C:\Scripts\UserUPN.csv" -Encoding UTF8 -NoTypeInformation
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by BucketheadFan Thursday, February 28, 2019 8:19 PM
    Thursday, February 28, 2019 6:37 PM

All replies

  • Please do not post colorized code as ist it almost unreadable. Use the code posting tool instead provided on the edit bar of the post editor. Thanks.

    $users = Import-Csv -Path 'C:\Scripts\UPNUserlookup.csv'
    ForEach($user in $users){
        Get-ADUser -identity $user -Properties UserPrincipalName | Select-Object -Property Name,UserPrincipalName

    ;-) ... almost .... try again.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''




    • Edited by BOfH-666 Monday, February 25, 2019 6:13 PM
    Monday, February 25, 2019 5:58 PM
  • Actually the UserPrincipalName property is a default property (like the Name property). It shouldn't be necessary to specify it with the -Properties parameter. I suspect something else is wrong, like the User in the CSV does not uniquely identify the user. It should be either the sAMAccountName or distinguishedName (or SID or GUID). It cannot be the Name (unless that matches sAMAccountName).

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Monday, February 25, 2019 6:43 PM
  • What is the user called in the csv?  I'm assuming the heading is "user".

    $users=Import-Csv -Path C:\Scripts\UPNUserlookup.csv
    ForEach
    ($user in $users){Get-ADUser -identity $user.user|
      Select-Object Name,UserPrincipalName}

    • Edited by JS2010 Tuesday, February 26, 2019 9:46 PM
    • Marked as answer by BucketheadFan Thursday, February 28, 2019 6:18 PM
    Monday, February 25, 2019 8:38 PM
  • The column header was username and I changed it to user and now it works. I am not sure how I missed that but thank you for your reply and fixing my simple script.
    Thursday, February 28, 2019 6:20 PM
  • So one more question since I don't understand the exact way to nest this/combine the commands how do I get this to write the output to a CSV it doesn't appear as simple as adding a pipe or another set of brackets to Write-Output 'C:\scripts\CSVoutput.csv'

    Thanks again for everyones help.

    Thursday, February 28, 2019 6:27 PM
  • This is one way.

    $Results = @()
    $users = Import-Csv -Path C:\Scripts\UPNUserlookup.csv
    
    ForEach ($user in $users)
    {
        $ADUser = Get-ADUser -identity $user.user | Select-Object Name, UserPrincipalName
        $Results += New-Object PsObject -Property @{
            Name = $ADUser.Name
            UserPrincipalName = $ADUser.UserPrincipalName
        }
    }
    $Results | Export-CSV -Path "C:\Scripts\UserUPN.csv" -Encoding UTF8 -NoTypeInformation
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by BucketheadFan Thursday, February 28, 2019 8:19 PM
    Thursday, February 28, 2019 6:37 PM
  • If it was a plain text file of usernames, it could be as simple as:

    get-content user.txt | Get-ADUser | Select-object Name,UserPrincipalName |
      export-csv user2.csv
    • Edited by JS2010 Thursday, February 28, 2019 7:48 PM
    Thursday, February 28, 2019 7:17 PM
  • This works perfectly, thank you for your help. Most appreciated. Do you have any advice on the best way to get better at Powershell and learn the scripting logic? Thank you again.
    Thursday, February 28, 2019 8:19 PM