How to Write Variables to a File in PowerShell? [6 Methods]

Do you want to write variables to a PowerShell file? I will show you different methods to write variables to a file in PowerShell.

To write a variable to a file in PowerShell, you can use the Out-File cmdlet, which is straightforward and effective for basic output needs. For example, $variable | Out-File -FilePath “C:\MyFolder\file.txt” writes the content of $variable to the specified text file. If you need to append data instead, use Add-Content, like so: Add-Content -Path “C:\MyFolder\file.txt” -Value $variable. For advanced scenarios requiring performance and encoding control, leverage the .NET StreamWriter class.

How to Write Variables to a File in PowerShell?

There are various methods to write variables to a file in PowerShell.

1. Using the Out-File Cmdlet

The Out-File cmdlet is a built-in PowerShell command that allows you to direct the output to a file. It’s a straightforward way to save data from a variable to a text file.

Here is an example.

$variable = "This is a test string."
$variable | Out-File -FilePath C:\MyFolder\output.txt

This script stores a string into a variable and then pipes the variable’s content to Out-File, specifying the file path where the output should be saved.

I executed the above PowerShell script, and you can see the output in the screenshot below:

Write Variables to a File in PowerShell

2. Redirection Operator

PowerShell also allows the use of the redirection operator > to write output to a file in PowerShell. This is similar to the redirection operator in other shells like Bash.

Here is another example:

$variable = "Another test string."
$variable > C:\MyFolder\output.txt

The above line will write the content of $variable to the specified file. If the file already exists, it will be overwritten.

3. The Add-Content Cmdlet

If you want to append data to a file in PowerShell rather than overwriting it, you can use the Add-Content cmdlet. This is particularly useful for logging purposes where you want to retain previous entries.

Here is a full PowerShell script.

$variable = "Appending this text to the file."
Add-Content -Path C:\MyFolder\output.txt -Value $variable

This PowerShell cmdlet adds the string from $variable to the end of the file specified.

You can see the output in the screenshot below:

How to Write Variables to a File in PowerShell

4. The Set-Content Cmdlet

Another cmdlet that is useful for writing to files in PowerShell is Set-Content. This cmdlet is similar to Out-File, but it is often faster and provides more control over the character encoding.

Here is an example:

$variable = "Writing this string with Set-Content."
Set-Content -Path C:\MyFolder\output.txt -Value $variable

The PowerShell Set-Content cmdlet writes the content to the file, replacing any existing content in the file.

5. Using the StreamWriter .NET Class

For more advanced file operations, PowerShell can access the .NET Framework’s StreamWriter class. This method is useful when you need to write to files in a more controlled or performance-sensitive scenario.

Here is the PowerShell script.

$variable = "Using StreamWriter for file output."
$stream = [System.IO.StreamWriter] "C:\MyFolder\output.txt"
$stream.WriteLine($variable)
$stream.Close()

This script creates a new StreamWriter object and uses its WriteLine method to write the variable’s content to the file. It’s important to close the stream after writing to release the file.

6. Exporting to CSV or JSON

PowerShell can also export variables to structured data files like CSV or JSON, which can be very useful for data interchange or further processing with other tools.

Export to CSV:

$variable = @(
    @{Name='John'; Age=30},
    @{Name='Jane'; Age=25}
)
$variable | Export-Csv -Path C:\MyFolder\output.csv -NoTypeInformation

This script takes an array of hash tables and exports it as a CSV file.

Export to JSON:

$variable = @{
    Name = 'John'
    Age = 30
}
$variable | ConvertTo-Json | Set-Content -Path C:\MyFolder\output.json

This script converts a hash table to JSON format and writes it to a file in PowerShell.

Conclusion

Writing variables to files in PowerShell is a very common requirement, and we can use any of the methods like Out-File or Set-Content for simple tasks, Add-Content for appending data, the redirection operator for quick outputs, StreamWriter for performance-critical applications and structured data exports like Export-Csv and ConvertTo-Json for interoperability.

In this PowerShell tutorial, I have explained how to write variables to a file in PowerShell using different methods.

You may also like: