PowerShell Add-Content with multiple variables to columns


Using Add-Content to input multiple variables to a csv file will write each variable to the csv in a new row, such as the following:


$name, $dateTime, $task | Add-Content $csv  -Force



I would like to loop the above code with new variable values each time but would prefer the variables to input to new columns for results such as the following:

$name $dateTime $task
$name $dateTime $task
$name $dateTime $task

How can I modify my code to input each of the 3 variables into a new column, then on the next pass, input the 3 new values on the next row across columns 1,2 and 3 like the above example?



Using a suggestion from @TessellatingHeckler, I used Export-CSV -Append and created a pscustomobject like so:

$logData = @(
  Name = $name
   DateTime = $dateTime
    Task = $task
$logData | Export-Csv $csv -Append -NoTypeInformation

By doing so I achieved two desirable effects; column headers and each variable is written to it's own cell per each iteration of the loop.


Name  DateTime  Task
$name $dateTime $task
$name $dateTime $task

Thanks, everyone.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


Add multiple variables(columns) in financial dataset with time/amount information

Multiple regex matches in Powershell - Output with Add-Content in the same line

Add multiple columns based on unique entries in other column in csv with powershell

ForEach with Multiple Variables - Powershell

Fetching content order by multiple columns

Strip certain content of columns in multiple columns

Check content of multiple columns of one row and add new column with value depending on contents

Drupal multiple variables in block content

bash: Join content of multiple variables

Dynamic modal content with multiple variables

Summarizing multiple columns by two variables

Getting multiple columns selected into variables

Categorical variables into multiple columns (2)

How to add new columns to a dataframe if common variables are found in multiple other dataframes

Multiple selection of two tables - multiple columns content

Join multiple columns from multiple variables?

pandas add multiple columns with apply

How to add multiple columns with purrr?

How to add multiple columns to a DataFrame?

add multiple columns to an xts object

Add quantiles to dataframe as multiple columns

add multiple columns to pandas dataframe

Add multiple columns and rows to a table

Add total of multiple counted columns

How to add method to a multiple variables

Using reactivity to add multiple variables

Multiple Variables (in columns), multiple Years(in columns) to reshape to flatfile in R

How to add content to specific grid columns?

Separate cell content after "-" for multiple columns at once

TOP Ranking