How To Write Array To File In PowerShell?

Writing an array to a file in PowerShell is a very common requirement and very useful in scenarios such as logging, data export, or simply saving data for later use. PowerShell provides several ways to write an array to a file. In this PowerShell tutorial, I will explain how to write an array to file in PowerShell using various methods.

To write an array to a file in PowerShell, you can use the Out-File cmdlet, which directs the output to a text file. For example, $array | Out-File -FilePath ‘output.txt’ will write the contents of $array to ‘output.txt’. This is an efficient way to save array data to a file for later use or as a log.

How To Write Array To File In PowerShell

An array is a data structure that stores a collection of items. These items can be of the same type or different types. In PowerShell, you create an array by assigning multiple values to a variable, like so:

$data = @("apple", "banana", "cherry")

Now, let us see how we can write the above array to a file in PowerShell using various methods.

Method 1: Using Out-File

The simplest way to write an array to a file in PowerShell is by using the Out-File cmdlet. This cmdlet sends output to a file. You can use it to write an array to a text file as follows:

# Create an array
$fruits = @("apple", "banana", "cherry")

# Write the array to a text file
$fruits | Out-File -FilePath "C:\Bijay\fruits.txt"

This will create a text file at C:\Bijay\fruits.txt with each element of the array on a new line.

You can see in the screenshot below that it created the txt file after I executed the PowerShell script using VS code.

How To Write Array To File In PowerShell

Method 2: Using Redirection Operators

Another method to write an array to a file is by using redirection operators. The > operator is used to write output to a file, overwriting any existing content. The >> operator appends the output to the end of the file.

# Create an array
$vegetables = @("carrot", "potato", "onion")

# Write the array to a file, overwriting existing contents
$vegetables > "C:\temp\vegetables.txt"

# Append more items to the file
$vegetables += "garlic"
$vegetables >> "C:\temp\vegetables.txt"

The first command will create a file with the vegetables listed, and the second command will append “garlic” to the end of the file.

Method 3: Using Set-Content

The PowerShell Set-Content cmdlet is similar to Out-File, but it is designed for string data and provides more control over the character encoding.

# Create an array
$colors = @("red", "green", "blue")

# Write the array to a file
$colors | Set-Content -Path "C:\temp\colors.txt"

This will write the colors to the file, with each color on a new line.

Method 4: Exporting to CSV

If you have structured data that you want to export in a CSV format, you can use the Export-Csv cmdlet. This is useful for arrays of custom objects or hashtables where each element represents a row with columns of data.

# Create an array of hashtables
$products = @(
    @{Name="Table"; Price=50},
    @{Name="Chair"; Price=25},
    @{Name="Lamp"; Price=20}
)

# Export the array to a CSV file
$products | Export-Csv -Path "C:\temp\products.csv" -NoTypeInformation

The Export-Csv cmdlet will create a CSV file where each hashtable in the array becomes a row, and each key-value pair becomes a column.

Method 5: Using Add-Content

The Add-Content cmdlet is another way to append data to a file in PowerShell. Unlike the >> operator, it allows you to specify the encoding.

# Create an array
$numbers = 1..10

# Append the array to a file
$numbers | Add-Content -Path "C:\temp\numbers.txt"

Each number will be appended to the file on a new line.

Notes:

  • When writing to a file, ensure that the file path is correct and that you have the necessary permissions to write to that location.
  • The Out-File, Set-Content, and Add-Content cmdlets allow you to specify the character encoding of the output file using the -Encoding parameter. This can be important if you’re working with non-ASCII characters.
  • If you’re working with complex data structures, consider converting them to a simple array or a list of strings before writing to a file to keep the process straightforward.
  • Always use the -NoTypeInformation parameter with Export-Csv unless you specifically need the type information in your CSV file.

Conclusion

Writing an array to a file in PowerShell can be accomplished in several ways. In this PowerShell tutorial, I have explained different ways to write array to file in PowerShell like:

  • Using Out-File
  • Using Redirection Operators
  • Using Set-Content
  • Exporting to CSV
  • Using Add-Content

You may also like: