PowerShell Out-file prevents overwriting of files _powershell

Source: Internet
Author: User

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!   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.