Danijel-James W
Danijel-James W

Reputation: 1506

PowerShell get CSV and add column from data query

Have a CSV that has headers "SamName", "Email", "UPN"

Each record is how it shows in AD currently.

I am trying to add a 5th column with the ObjectGUID as follows:

$CsvFile = Import-Csv C:\test\file.csv
$CsvFile | ForEach-Object -Process { `
    $data1 = Get-ADUser -Identity $_."SamName" -Properties * | Select-Object ObjectGUID
    $_ | Add-Member -MemberType NoteProperty -Name UserGUID -Value $data1 -PassThru } `
    Export-CSV C:\test\file2.csv

When I open the CSV, the new column is called "UserGUID" as I had expected.

The GUID's that show are like this:

@{ObjectGUID=2e9dddec-f95d-4c67-a374-b102ce7e6f74}

Why don't they just show 2e9dddec-f95d-4c67-a374-b102ce7e6f74 in the column instead?

A second question, is why at the top of the Csv do I see #TYPE System.Management.Automation.PSCustomObject ?

Upvotes: 2

Views: 420

Answers (1)

Kory Gill
Kory Gill

Reputation: 7163

Two guesses to answer your questions...

Select-Object -ExpandProperty ObjectGUID

and

Export-CSV C:\test\file2.csv -NoTypeInformation

See Get-Help for various commands to see all the options.

Upvotes: 2

Related Questions