Skip to content Skip to sidebar Skip to footer

You Are Working at the Command Line and Want to Add the Read Only

When you demand to ensure a file isn't changed nether whatsoever circumstances in Windows, it's fourth dimension to make it read-merely. Setting a file equally read-merely or write-protecting a file prevents whatsoever changes any. If you don't know how to gear up files as read-merely and demand to, yous're in the right place!

In this tutorial, you're going to learn how to prepare and remove the read-only attribute in File Explorer, the command-line (cmd), and via PowerShell!

Permit's get going.

Prerequisites

This mail will exist a hands-on tutorial. If you'd like to follow along, be sure you have the following:

  • A Windows PC. This tutorial will apply Windows ten and Windows PowerShell v5.ane.
  • A file to toggle the read-just attribute on. This tutorial will use a file located at C:\Shared\readme.txt.

How to Brand a File Read-Merely (and Writable) in File Explorer

At that place are a few different ways to set a file every bit read-only merely let's beginning out this tutorial past outset roofing how to make it happen via Windows File Explorer.

The read-just attribute is stored every bit a metadata property rather than in the content of the file. The property can exist seen through Windows File Explorer.

Assuming that you're on the desktop of your Windows PC:

1. Open up Windows File Explorer and navigate to the folder that stores the file you'll be working with (C:\Shared) in this example.

2. Right click the file and click Properties to open the file'due south Properties box. This box contains various metadata about that particular file such as size, when it was created, modified, etc. It also has file attributes.

Click the read-only checkbox in the Attributes group to write-protect the file or prepare it as read-only, click Apply to assign the read-only attribute, and OK to shut the dialog box.

The properties box of the file readme.txt. Note the location of the Read-only attribute check box and the Apply button.
The properties box of the file readme.txt. Annotation the location of the Read-only attribute check box and the Apply button.

Similarly, to remove the read-only aspect from the file, uncheck the Read-simply checkbox.

How to Brand a File Read-Only (and Writable) via Command Line

File attributes aren't merely exposed via File Explorer. You can also manipulate file attributes including the read-but attribute via the command line. To do so, y'all must use a command chosen attrib.

1. Open up upward a control prompt (cmd.exe).

2. Navigate to the binder the file is in.

3. Run attrib with no parameters. The attrib command will immediately look for all files in the working directory and return all files and the enabled file attributes. You tin can encounter below the readme.txt file does non accept the read-merely aspect enabled.

Archive attribute enabled (A) but no read-only attribute (R)
Archive attribute enabled (A) but no read-only attribute (R)

four. Now, run attrib to assign the read-only aspect. The +r parameter is primal here. This parameter tells Windows to prepare the read-only attribute on the file.

5. Run attrib again with no parameters to verify the read-only attribute was applied. You can now run across in the screenshot below the R characterization shows up indicating the read-just aspect has been applied write-protecting the file.

Read-only attribute (R) is now enabled
Read-just attribute (R) is now enabled

Similarly, to remove the read-merely aspect use the -r parameter.

How to Make a File Read-Simply (and Writable) with PowerShell

If you lot're not a fan of the GUI or the control prompt or desire to use have a little more ease of control, wait no further than PowerShell! Let's now learn how to brand a file read-simply with PowerShell!

  1. Open Windows PowerShell.

2. Run the Get-Item cmdlet providing the path to the file you're toggling the read-only attribute on. The Attributes property returned contains each file attribute that'southward currently enabled.

            get-item -Path "c:\shared\readme.txt" | format-table name, attributes          
The Archive attribute is only enabled.
The Annal attribute is merely enabled.

Using the Format-Table cmdlet is not necessary but by doing then, tells PowerShell to display just the 2 properties you're interested in in an easy-to-view format (Proper name and Attributes)

iii. Assign the read-only attribute past calculation the ReadOnly attribute to the Attributes property every bit shown below.

            ## Capture the file object in the $file variable  $file = Get-Detail -Path "c:\shared\readme.txt" ## Not to overwrite the existing attributes, create a new array containing ## the current file attributes while adding the ReadOnly attribute and assign ## that array as the new value of the Attributes property  $file.Attributes = @($file.Attributes,"ReadOnly")          
Assigning the Read Only Attribute
Assigning the Read Just Attribute

iv. Now, view the Attributes holding of the $file object and yous'll see that you've now applied the ReadOnly attribute to the file while maintaining whatsoever existing attributes that existed on the file beforehand. You can also run Get-Item once again to view the same results.

Viewing the Attribute Property
Viewing the Aspect Property

To remove the ReadOnly attribute from the file, follow the verbal same approach just this time set the value of the Attributes holding to Normal east.chiliad. $file.Attributes = "Normal".

Conclusion

You should now know how to fix a file equally read-only and vice versa. Setting a file as read-only is meant to prevent adventitious changes. But know that, setting a file equally read-only is not necessarily a security measure and does non prevent access to the file or someone from merely changing the attribute themselves.

Armed with this new knowledge of protecting files from beingness accidentally overwritten, do you lot take any files you'd like to protect?

fernandesabour1978.blogspot.com

Source: https://adamtheautomator.com/how-to-make-a-file-read-only/

Отправить комментарий for "You Are Working at the Command Line and Want to Add the Read Only"