This article describes how to prevent the system from automatically overwriting existing files when you use PowerShell to write files.
When we use scripts for file processing, we may not want to overwrite existing files with Ou-file-generated files. So how does it happen? Is there any argument in the Out-file cmdlet that prevents Out-file from silently overwriting existing files?
The answer is-noclobber parameter.
Noclobber parameters
When out-file output to a file, if the-noclobber parameter is used, the system cannot succeed if it encounters an existing file. The following example shows that when d:\1.txt already exists, the system will make an error when using Out-file output to the file.
Copy Code code as follows:
PS c:\users\spaybow> "" | Out-file D:\1.txt
PS c:\users\spaybow> "" | Out-file D:\1.txt-NoClobber
Out-file: File "D:\1.txt" already exists.
Location: 1 Characters: 14
+ "" | Out-file <<<< D:\1.txt-NoClobber
+ categoryinfo:resourceexists: (d:\1.txt:string) [Out-file], IO
Exception
+ Fullyqualifiederrorid:noclobber,microsoft.powershell.commands.outfilec
Ommand
In addition, we know that the-append parameter is used to specify that strings are appended to the file. If-append and-noclobber parameters are specified at the same time, will there be a conflict? The answer is that the system attaches a string to the file. The demo is as follows:
PS c:\users\spaybow> "Hello" | Out-file D:\1.txt
PS c:\users\spaybow> "PowerShell" | Out-file D:\1.txt-NoClobber-Append
PS c:\users\spaybow> Type D:\1.txt
Hello
PowerShell
About PowerShell use Out-file write files prohibit overwriting existing files, this article on the introduction so much, I hope to help you, thank you!