Set-ContentCmdlet in PowerShell
Set-ContentCmdlet to Write Text Content to a Single File
Set-ContentCmdlet to Replace the Text Content in Multiple Files
Set-Contentto Pass the Text Content Through a PIPE
Sometimes, we might need to write the same text content into multiple text files. PowerShell provides a useful cmdlet to write text content via the command line.
In this article, we will be focusing on the
Set-Content Cmdlet in PowerShell
Set-Content cmdlet writes new text content or replaces the existing text in a file. This is more similar to the
> operator in Linux.
Set-Content cmdlet differs from the
Add-Content string processing cmdlet, which appends text to a specified file. The
sc can be used as the alias for the
Set-Content [-Path_to_file] <string> [-Value] <Object> [-PassThru] [-Filter <string>] [-Include <string>] [-Exclude <string>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-NoNewline] [-Encoding <Encoding>] [-AsByteStream] [-Stream <string>] [<CommonParameters>]
The command takes quite a large number of arguments, and the purpose of a few has been described in the following section.
||You can specify an existing text file path or create a new one in the given path.|
||This parameter is used to pass the text content you will write.|
||The text encoding of the target file is set to
||The items that need to be excluded in this operation are specified as a string array.|
||This is the opposite of the
-Value parameter is usually used to specify the text content written in the file. In addition, you can pipe the text content from another object as well.
If you pass a non-string object to the
Set-Content command, it will first convert the non-string object to a string and writes it to the file.
Set-Content Cmdlet to Write Text Content to a Single File
Let’s write text content to a file called
sometext.txt that is not currently existing. So, the Set-Content command would create this file first and write the content passed with the
Set-Content -Path D:\codes\sometext.txt -Value "This should create a new file called sometext.txt and write this text to it..."
Set-Content Cmdlet to Replace the Text Content in Multiple Files
Let’s assume we have three files
file1.txt, file2.txt, and file3.txt. We will replace the content in those three files with the following text.
Hello, i would be appeared in all the three files!
In this case, the
Set-Content command uses the wildcard characters to match the given three files.
Set-Content -Path D:\codes\file*.txt -Value "Hello, i would be appeared in all the three files!"
The three files match the pattern specified by
file* txt, which forces the command to match all the files starting with the
Set-Content to Pass the Text Content Through a PIPE
It is not necessary to specify the
-Value argument always. Instead, we can PIPE a text object to the
Set-Content as follows.
(Get-Content D:\codes\file1.txt).replace('all the three files!', 'file4.txt') | Set-Content D:\codes\file4.txt
Get-Content cmdlet reads the text content from the
file1.txt file. Then, it uses the
replace command to replace
all the three files! text with the
Finally, the constructed text would be written into the