0

I have a .csv template with an empty column called URL. I would like to update it every time I get a value for its user.

Here is what I have so far:

$file = "c:\Users\user\Documents\users.csv"

Import-Csv $file  -Delimiter ',' | % {

     $_.URL = $generated_url ; $_

} | Export-Csv "c:\Users\user\Documents\users_tmp.csv" -Delimiter ',' -notype

This updates all of the cells with the same value. Is there a workaround? Thank you

1 Answer 1

1

I assume you want to update just one row which corresponds to the current user.

To do that you have to select the row that has to be updated using the where-object clause

param(
 $username
)

$file = "c:\Users\user\Documents\users.csv"

Import-Csv $file  -Delimiter ',' | Where {$_.username -like $username} | % {

 $_.URL = $generated_url ; $_

} | Export-Csv "c:\Users\user\Documents\users_tmp.csv" -Delimiter ',' -notype

To save the CSV as excel as asked in the comment below, the best option is to save as CSV as above. Then use Excel COM libraries to convert it.

Example

$Excel = New-Object -comobject Excel.Application
$Excel.Visible = $False
$Excel.displayalerts=$False
$Workbook = $Excel.Workbooks.Open($CSVFileName)
$Workbook.SaveAs($Excelfilename,1)
$Excel.Quit()
If(ps excel){
  kill -name excel
}
Sign up to request clarification or add additional context in comments.

4 Comments

That's exactly what I wanted. So simple.. But my issue now is that when I do that for two users, every time it creates a new csv that includes only that one specific user. Is there a way to update the existing csv file? Thanks
I had to use the -append at the end. Thank you very much!
Would it be possible to update an exccel spreadsheet with those values? Rather exporting it in to a new csv
You can use Excel COM libraries to open and Save as xls . See code edit above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.