Add Column to CSV in Powershell

Adding a new column into a CSV boils down to these three steps:

  • Load CSV file into a PowerShell Array using Import-Csv cmdlet,
  • Add the new columns,
  • (Optional) Export the result into a new or existing CSV file using the Export-Csv cmdlet.

In this article, we will consider several cases through examples. These cases include: adding a column with a default value, creating a new column with specified values, adding a column based on an already existing column, and lastly, joining two CSV files into one based on a given field.

In the majority of the examples, we will use “employees_names.csv” with the following data

FName,LName,Salary
Allan,Smith,3000
Lewis,Walker,5600
Deborah,Shawn,12100
Smith,Rowe,2800
Kael,Fernandez,7200

The Import-Csv Cmdlet

Import-Csv cmdlet loads CSV data into a table-like custom object. Each column in the CSV becomes a property of the custom object, and the row values become the property values.

The following code loads the “employees_names.csv” shown above.

To create a column in a CSV, we will supply a new column/property name and provide property values using the Add-Member cmdlet, as shown in the following examples.

Example 1: Creating a New Column With a Specific Default Value

That can be achieved by running the following PowerShell code.

Script name: new_column_with_default.ps1

The code above creates a new column named “Col1” with -99 in all the rows. The -Force option is supplied if you want to overwrite an existing column.

Run PowerShell scripts by running the commands:

After running the code above, “employees_names.csv” will look like this.

Example 2: A New Column With Values Issued by the User

This can be done with the code below.

When you run, the code above your CSV file will be updated as follows.

Example 3: Create Two Columns Based on an Existing Column

In this example, we want to create “Increment” and “NewSalary” columns where Increment = 0.11*Salary (11% of Salary) and NewSalary=Salary+Increment.

Output:

Example 4: Creating a Column Based on Another Using If-statement.

In this example, we create a ” Remark ” column based on the Salary column. If Salary>10000, then Remark = “High”; if Salary < 7000, then the Remark is “Low”; otherwise, the Remark is assigned the value “Middle”.

Output:

Example 5: Joining two CSV files

Given two CSV files, this example will use Join-Object to join the two files into one using a common column.

Join-Object doesn’t come pre-installed in PowerShell. You can install it by running the following command on PowerShell:

Install-Module -Name Join-Object

We will join two CSV files – names.csv and roles.csv with the following contents.

File name: names.csv

id,Name,Year Employed
1,Allan,2007
2,Sherrie,2006
3,Ammon,2015
4,Caroline,2020
5,Alice,2014

File name: roles.csv

id,Role,Department
1,CEO,Management
2,CFO,Management
3,Managing director,Management
4,Data analyst,Data
5,Software developer,Technology

Using the code below, we can join the two files above using the common column (id).

And the result is as follows:

After going through the guide above, you should be able to create columns on CSV using PowerShell easily.