Write Array to CSV in PowerShell

This article will explain how to write an array to a CSV file in PowerShell.

Using the Export-Csv cmdlet

The Export-Csv cmdlet in PowerShell creates a CSV file of the given objects. But you cannot directly write an array to a CSV file correctly.

When you pipe the array to Export-Csv, the result will be similar to this.

Write Array to CSV file in PowerShell

The best way of exporting an array to a CSV file is to:

  • Create a PSObject from an array
  • Store PSObject in a new array
  • Pipe a new array to the Export-Csv cmdlet

In the above example, we have created the $array to write to a CSV file. The $newarr is an empty array which will be later used to store the PSObject. The $columns contain the heading label for the data.

We created the PSObject $obj using the New-Object cmdlet. Then in for loop, we used Add-Member to add $columns values in the Name property and $array values in the Value property of the PSObject.

The New-Object cmdlet is used to create an instance of a .NET Framework or COM object.

Add-Membercmdlet is used to add custom properties to the PowerShell objects.

After that, we filled $newarr with added data in $obj.

The $newarr contains the following data now.

At last, we piped an array $newarr to the Export-Csv cmdlet and created a CSV file output.csv in the current directory.

The -NoTypeInformation is used to remove the #TYPE information header in a CSV file.

The array is successfully exported to a CSV file.

Write array to CSV in PowerShell

Using the Set-Content cmdlet

For this method to work, you need to create an array like this:

If we use a comma to separate the multiple items, we can write it to a CSV file in the correct format.

To export this array to a CSV, you can use the Set-Content or Add-Content cmdlet.

That’s all about how to write an array to csv in PowerShell. If you have any confusion, let us know in the comments.

Was this post helpful?

Related Posts

  • Show hidden files in PowerShell
    13 January

    Show Hidden Files (dotfiles) with Windows PowerShell

    Table of ContentsUsing Get-ChildItem with the -Force ParameterUsing Get-ChildItem with the Where-Object cmdletUsing ls with the -Force ParameterUsing dir with -ah Switch Using Get-ChildItem with the -Force Parameter To show hidden files with other files, use the Get-ChildItem cmdlet with the -Force or -fo parameter. [crayon-63c469cbd72d1178905403/] [crayon-63c469cbd72d7886490731/] The Get-ChildItem is a PowerShell cmdlet that lists […]

  • Merge multiple CSV files in PowerShell
    13 January

    Merge Multiple CSV Files in PowerShell

    Table of ContentsUsing Import-Csv and Export-CsvUsing + operatorUsing the File Class Using Import-Csv and Export-Csv Before merging multiple .csv files, let’s write a script to see how many .csv files are in the specified directory. For that, create a variable called $sourcefiles containing a list of all the CSV files in the directory. Next, use […]

  • Multiline command powershell
    10 January

    PowerShell – Multiline Command

    Table of ContentsUsing BackticksUsing Pipeline OperatorUsing Specific Operators/Characters Using Backticks Use backticks to split multiline commands in PowerShell. [crayon-63c469cbd8011031504698/] Usually, we get an automatic line continuation in PowerShell when the command can’t syntactically be complete. However, if splitting a multiline command is required, then using backticks is preferred in such scenarios as presented above where […]

  • Call Function with Parameters in PowerShell
    08 January

    PowerShell – Call Function with Parameters

    Table of ContentsUsing Positional ParametersUsing Named Parameters Using Positional Parameters To call a function, use positional parameters separated by space. Please note that you need to pass parameters in sequence while calling the function. [crayon-63c469cbd8541562831039/] [crayon-63c469cbd8546145840903/] Microsoft’s PowerShell consists of a command-line shell and scripting language. It is based on the .NET framework for task […]

  • 08 January

    Press Any Key to Continue in PowerShell

    Table of ContentsSolutions for PowerShell Integrated Scripting Environment(ISE) and Command Line ConsoleUsing Read-Host CmdletUsing Message Box UISolutions for PowerShell Command Line ConsoleUsing ReadKey() Method with/without [void]Using RawUI.ReadKey() MethodUsing cmd /c pause CommandUsing timeout /t Command Solutions for PowerShell Integrated Scripting Environment(ISE) and Command Line Console Using Read-Host Cmdlet Use the Read-Host command to enable press […]

  • 08 January

    PowerShell – Unzip File

    Table of ContentsUsing Expand-Archive CmdletUsing System.IO.Compression.ZipFile NamespaceUsing 7Zip Module Using Expand-Archive Cmdlet Use the Expand-Achrive cmdlet to unzip a file/folder in PowerShell. [crayon-63c469cbd01c9737203095/] Please note that Expand-Archive cmdlet is available from PowerShell 5 onwards. Print envionment variable $PSVersionTable.PSVersion to check version of your PowerShell. We used the Expand-Achrive cmdlet to unzip a folder from the […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.