Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
2.5k views
in PowerShell by 66 69 85
edited by

A CSV file with multiple rows and custom values for each column is being added.

What is the PowerShell syntax for this script?

$fileSource = "D:\info\data.csv"

$name = "Mohammed"
$address = "MyAddress"
$phone = "0512345678"

#How to add these values in csv file?

 

I tried using Export-Csv with the '-append' option, but it didn't work.

 


1 Answer

1 like 0 dislike
by 66 69 85
selected by
 
Best answer

How to add rows to CSV file in PowerShell?

In PowerShell, we can use the Add-Content cmdlet to add your values and between each value add comma.

I suggest to put your values in separate variable to less the complexity.

$fileSource = "D:\info\data.csv"

$name = "Mohammed"
$address = "MyAddress"
$phone = "0512345678"

# Add variables in the $newRow without space between them
$newRow = $name,$address,$phone

# Add row in the csv file
$newRow | Add-Content -Path $fileSource
If you don’t ask, the answer is always NO!
...