Sharing PowerShell TIP on how to ADD an extra column to existing CSV file.
I have got this requirement many a times from Tableau or PowerBI team to add an extra column Date in existing CSV file.
Step 1 à
$data = Import-CSV c:\csvfile.csv
Import CSV file into a variable $data
Step 2 à
$getdate = get-date -format D
Step 3 à
$data | Select-Object *, @{n=”Date”;e={$getdate}} | Export-CSV c:\newcsvfile.csv -NoTypeInformation
ADD the computed date value in step 2 in select expression and export as new csv file.
Here is the result:
Now use this TIP to automate it in a script and make your PowerBI and Tableau or BI team happy :).
Thanks for reading….
Tech Wizard
Hello! I stumbled across your blog piece.
I’m assume the key to your one-liner’s effectiveness is the use of the asterisk ‘*’ as in Select-Object *, because you’re telling PowerShell ‘first of all, I’m including all of the CSV’s original contents’, then you’re using the format operator to define a new column, ‘Date’ to have the value of the variable $getDate.
Tested at my end; worked a treat. Thank you sir.