Home > Blockchain >  use where-object to find data, but want to add data to every row also the export to csv
use where-object to find data, but want to add data to every row also the export to csv

Time:06-16

Hi I have a script that reads a csv file, creates a json file, checks the users in the file against a service, then i get the result as a json file. I take that result and finds the users i csv file and creates a new file. I do that with a where-object

But i need to add some extra values on every user before i export it to csv

This is my 2 lines for finding users and then export

$matches = $users | where-object { $_.number -in $response.allowedItemIds } | Select-Object -Property Number,Surname,Forename,Emailaddress

$matches | Export-Csv -path $Saved$savefile -NoTypeInformation -Append

Is that possible or do i need to do a for each?

Cheers

CodePudding user response:

Assuming I've interpretted your question correctly, you should be able to use PowerShell's Calculated Properties for this purpose.

For example, if you wanted to add a field called "Date" and set the current Date/Time to each user row, you could do the following:

$matches = $users | where-object { $_.number -in $response.allowedItemIds } | Select-Object -Property Number,Surname,Forename,Emailaddress, @{Name="Date";Expression={Get-Date}}

The Expression value can either be a static value such as "StaticValue", a variable such as $i (useful if used as part of a loop, for example) or more complex value that is returned from other cmdlets (as in my example above)

  • Related