PowerShell Out-file How to write content to read-only files _powershell

Source: Internet
Author: User

This article describes how to force overwriting a read-only file when you use PowerShell to write a file.

D:\2.txt is a read-only file, and if I run a out-file output statement, I get an error message:

Copy Code code as follows:

PS c:\users\spaybow> "" | Out-file-append D:\2.txt
Out-file: Access to Path "D:\2.txt" was denied.
Location: 1 Characters: 14
+ "" | Out-file <<<<-append D:\2.txt
+ Categoryinfo:openerror: (:) [Out-file], Unauthorizedaccessexc
Eption
+ Fullyqualifiederrorid:fileopenfailure,microsoft.powershell.commands.ou
Tfilecommand

"Access to the path" D:\2.txt "is denied" because this 2.txt is a read-only file.

Sometimes, we can not control so much, must be mandatory coverage, how can God block kill God, Buddha block kill Buddha? Fortunately, Out-file also provides a-force parameter that, after joining, can be written to the content regardless of whether the file is read-only.

Copy Code code as follows:

PS c:\users\spaybow> "" | Out-file-append-force D:\2.txt
PS c:\users\spaybow>

No error is the implementation of success!

Also notice here that after executing the command to write the file, check out the d:\2.txt, and I find that it is still a read-only state. This state has not been corrupted because it was written to the content.
Also, if the current user does not have write access to the file, the content cannot be written to the file even if the-force parameter is used.

about how PowerShell to write content to read-only file, this article introduces so much, 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.