Overwrite File in PowerShell

Overwrite File in PowerShell

Using Set-Content Cmdlet

Use the Set-Content cmdlet to overwrite a file in PowerShell.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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 while Out-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 while Set-Content is running. This saves a more straightforward representation in the text file.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *