Table of Contents
Using Set-Content
Cmdlet
Use the Set-Content
cmdlet to overwrite a file in PowerShell.
1 2 3 |
Set-Content -Path E:\Test\File1.txt -Value "Greetings" |
1 2 3 |
Greetings |
Initially, the File1.txt
file had Hi!
as its content, but later it was overwritten, and now it contains the content as shown in the above output. The Set-Content
is known as the string-processing cmdlet, which we use to overwrite the content of an existing file. For example, let’s say you provided a path for a file that does not exist; then, Set-Content
will create that file and write the specified content.
You might confuse it with the Add-Content
cmdlet, but there is a crystal clear difference between the Set-Content
and Add-Content
cmdlets. The Set-Content
replaces existing content while Add-Content
appends content to the specified file.
We used the -Path
parameter to specify the path of the file/item that will receive the content sent using the -Value
parameter. Note that we are allowed to use wildcard characters while giving the file path. For example, we want to overwrite each text file whose name starts with the word File
; see the following example.
1 2 3 |
Set-Content -Path E:\Test\File*.txt -Value "Hello World" |
1 2 3 |
Hello World |
Here, we used an asterisk (*
) to overwrite all text files whose filenames have any number of characters after the word File
; remember, each file name must start with File
and end with .txt
; in between, we can have any number of characters, this is what the *
is supposed to do. All the above example commands use the -Value
parameter to send content to the Set-Content
cmdlet but did you know that we can send content using the pipeline? How? See the following example.
1 2 3 |
"Write This Content" | Set-Content -Path E:\Test\File1.txt |
1 2 3 |
Write This Content |
Here, we used pipeline (|
) to forward content to the Set-Content
, which will be overwritten in the given text file. We can use the Get-Content
cmdlet as follows to look at the latest content of the File1.txt
file.
1 2 3 |
Get-Content -Path E:\Test\File1.txt |
1 2 3 |
Write this Content |
Suppose we have the requirement to ask the users if they still want to overwrite the file. In that case, we can use the -Confirm
parameter as given below to prompt the user and ensure that s/he still want to overwrite the file.
1 2 3 |
Set-Content -Path E:\Test\File1.txt -Value "Greetings" -Confirm |
1 2 3 4 5 6 7 8 9 10 11 |
Confirm Are you sure you want to perform this action? Performing the operation "Set Content" on target "Path: E:\Test\File1.txt". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y Confirm Are you sure you want to perform this action? Performing the operation "Clear Content" on target "Item: E:\Test\File1.txt". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y |
In contrast, if we are required to overwrite the file at any condition, we can use the -Force
parameter as follows. This parameter forces the cmdlet to overwrite the file even if the file is read-only. Note that this parameter doesn’t override the security restrictions.
1 2 3 |
Set-Content -Path E:\Test\File1.txt -Value "Greetings" -Force |
1 2 3 |
Greetings |
We can also use the sc
alias of the Set-Content
cmdlet to reduce keystrokes. You can learn more valuable parameters at this page to use them according to your project needs.
Using Out-File
Cmdlet
Use the Out-File
cmdlet to overwrite a file in PowerShell.
1 2 3 |
"Some content" | Out-File -FilePath E:\Test\File1.txt |
1 2 3 |
Some content |
First, we wrote the content that we wanted to write in the file. Then, this content was forwarded to the Out-File
cmdlet using pipeline (|
), which we used to send the output (received from the previous process) to the given file. It implicitly uses the formatting system of PowerShell to write to the specified file.
While using the Out-File
cmdlet, the specified file will get the exact display representation as the terminal/console, which means it may not be suitable for various programmatic processing until and unless all input objects are strings.
Like the Set-Content
, the Out-File
will also create the specified file if it does not exist and write the content into it. It also has -Force
and -Confirm
parameters that work the same as we learned using the Set-Content
cmdlet. In addition, the Out-File
has another exciting parameter, -NoClobber
.
The -NoClobber
prevents the existing file from being overwritten and informs the user that the file s/he intends to overwrite already exists. So, if you have a situation not to overwrite already existing files, don’t forget to use the -NoClobber
parameter because the default behaviour of the Out-File
cmdlet is to overwrite the file without warning.
1 2 3 |
"Some content" | Out-File -FilePath E:\Test\File1.txt -NoClobber |
The above command will generate a warning saying, Out-File: The file 'E:\Test\File1.txt' already exists.
NOTE: While using the
Out-File
cmdlet, we are not allowed to use wildcard characters while specifying the path using the-FilePath
parameter; otherwise, it will generate a warning.
Further reading:
Using Copy-Item
Cmdlet
Use the Copy-Item
cmdlet to copy the whole content of the source file and overwrite the destination file in PowerShell.
1 2 3 |
This is the source sample text. |
1 2 3 |
This is the destination sample text. |
1 2 3 |
Copy-Item -Path E:\Test\File1.txt -Destination E:\Test\File2.txt -Force |
1 2 3 |
This is the source sample text. |
We have already learned about the -Force
parameter using the Set-Content
cmdlet. Here, -Path
and -Destination
parameters denote the path of the source file and destination file, respectively. We used Copy-Item
to copy the source file’s contents (File1.txt
) and paste it into the destination file (File2.txt
) by overwriting its previous content.
The Copy-Item
allows us to use wildcard operators while specifying the path but be aware of what is happening behind the scenes to avoid any unexpected results. For instance, if you think the following code will copy the content from multiple source files and paste it into the destination file, you should correct your understanding.
1 2 3 |
Copy-Item -Path E:\Test\SampleFile*.txt -Destination E:\Test\File2.txt -Force |
It is because we learned that the Copy-Item
cmdlet pastes the new content by overwriting the previous content, which means it will paste the content for every source file by overwriting the previous content. This way, we will only have the content of the last source file, not all source files.
We have learned three different ways to overwrite the file, but when to use which approach? To make a better decision, keep the following points in mind:
- We use
Copy-Item
to copy files and directories. - We use
Out-File
to send string and non-string objects to a text file and can be used to append or create an existing file. Another application can read a file whileOut-File
is running. It saves the output’s fancy formatting in the text file, as seen in the terminal. - We use
Set-Content
to send string-type content to a text file. If the content is non-string, this cmdlet will convert it to a string before writing/sending it to a text file. Other applications cannot read the file whileSet-Content
is running. This saves a more straightforward representation in the text file.