How to Use PowerShell Set-Content cmdlet?

In this tutorial, I will explain how to use the Set-Content cmdlet in PowerShell. As a developer, I once faced a situation where I needed to update configuration files across multiple servers in the USA. I did this using the PowerShell Set-Content cmdlet. In this tutorial, I will explain how to use the PowerShell Set-Content cmdlet with some practical examples.

To use Set-Content in PowerShell, specify the file path and the content you want to write. For example, Set-Content -Path "C:\Projects\USAConfig.txt" -Value "ServerName=NewYorkServer" writes the string “ServerName=NewYorkServer” to the specified file. If the file does not exist, Set-Content will create it. This cmdlet is useful for creating or updating files with new content efficiently.

What is Set-Content in PowerShell?

The Set-Content cmdlet in PowerShell is used to write or replace the content in a file. It is a useful cmdlet for managing file content, and it can handle text files, XML files, and more. Whether you need to create new files or update existing ones, Set-Content is the cmdlet for the job.

Now, let me show you some practical usages.

Basic Usage of PowerShell Set-Content

To use Set-Content, you need to specify the file path and the content you want to write. Here is a simple example and the complete PowerShell script.

Set-Content -Path "C:\Projects\USAConfig.txt" -Value "ServerName=NewYorkServer"

In this example, we are writing the string “ServerName=NewYorkServer” to a file named USAConfig.txt located in the C:\Projects directory. If the file does not exist, Set-Content will create it.

I executed the above PowerShell script using VS code. You can see it created the file in the specified folder and wrote the details in the text file. Here is the complete screenshot.

PowerShell set-content

Check out PowerShell Test-Path

Write Multiple Lines using Set-Content

Now. let me show you how to write multiple lines using Set-Content in PowerShell.

To write multiple lines to a file, you can pass an array of strings to the -Value parameter. For example:

$lines = @(
    "ServerName=NewYorkServer",
    "Database=ChicagoDB",
    "Port=8080"
)
Set-Content -Path "C:\Projects\USAConfig.txt" -Value $lines

This command writes three lines to the USAConfig.txt file.

Here is the exact output in the screenshot below:

how to use PowerShell set-content

Check out How to Use PowerShell new-item

Using Set-Content with Different Encodings

Sometimes, you might need to write content in a specific encoding. The Set-Content cmdlet supports various encodings, such as UTF8, ASCII, and Unicode. Here’s how to specify the encoding:

Set-Content -Path "C:\Projects\USAConfig.txt" -Value "ServerName=NewYorkServer" -Encoding UTF8

This command writes the content with UTF8 encoding.

PowerShell Set-Content Example: Updating Configuration Files

Now, let me show you a real world example of using the PowerShell Set-Content cmdlet.

Let’s say you have a configuration file that needs to be updated with new server information. Here’s how you can use Set-Content to automate this task:

$serverInfo = @(
    "ServerName=LosAngelesServer",
    "Database=SanFranciscoDB",
    "Port=9090"
)
Set-Content -Path "C:\Projects\USAConfig.txt" -Value $serverInfo

This script updates the USAConfig.txt file with new server information for a server located in Los Angeles and a database in San Francisco.

Conclusion

The Set-Content cmdlet in PowerShell is used for writing and updating file content. Whether you need to create new files, update existing ones, or manage configuration files across multiple servers, Set-Content can help you automate these tasks efficiently. In this tutorial, I explained how to use the PowerShell Set-Content cmdlet with some real examples.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.