Luke K Luke K - 4 months ago 8
PowerShell Question

Putting CSV Data as is into a New CSV

I have a CSV file with two columns; Identitiy, and User. the User column contains UserPrincipalNames, and the Identity column contains a name. What I'm trying to do is to take the Userprincipalnames and get the Displayname from them, which I am able to do.

What I can't figure out it how to get each rows Identity (which is already in the csv I imported) to be displayed alongside the newly found Displayname. I'm not using the Identity column to get anything, I just want to display the values again.

The code I'm using is this:

$accounts = ipcsv 'D:\code-folder\small-results.csv'

$accounts | % {
$Identity = $_.Identity
$User = $_.User
Get-MsolUser -UserPrincipalName $User | select Displayname
}


This gets the
displayname
, but nothing more. I tried placing the
$Identity
variable in the select, but that didn't work. I feel like there must be a simple solution to this.

For example,a row in the CSV file would look like this

But I want the output to look like this, but the output I'm getting leaves out the Identity, and I don't know how to have it added.

Answer

You can make a custom column with Select-Object (select):

$accounts = ipcsv 'D:\code-folder\small-results.csv'

$accounts | % {
    $Identity = $_.Identity
    $User = $_.User
    Get-MsolUser -UserPrincipalName $User | select @{n='Identity';e={$Identity}},Displayname
}