Sam
Sam

Reputation: 307

How to add variables to a csv file with powershell?

I'm trying to add a specific user to a csv file. The file must have Name, Username, Email and Date as headers and when a user is added the specific variables are placed in the correct column. Here is what I have so far:

  Add-content -path C:\...\file.csv -value '"name","username","email","date"'
  $user = @($name,$username,$email,$date)
  Add-content -path C:\...\file.csv -value $user

(The variable name, email, username and date are defined previously and are strings)

When I run the command

 import-csv -path C:\...\file.csv

It returns the variables defined in the column Name instead of each variable being placed in it's specific header. Why is this happening?

Upvotes: 1

Views: 4999

Answers (1)

Sam
Sam

Reputation: 307

 New-Object -TypeName PSCustomObject -Property @{
    Name=$name
    Username=$username
    Email=$email
    Date=$date
 }|Export-Csv -path C:\...\file.csv -NoTypeInformation -Append

Upvotes: 2

Related Questions