How to Make a File Read-Only and Vice Versa

Published:5 May 2021 - 3 min. read

Jeff Stokes Image

Jeff Stokes

Read more tutorials by Jeff Stokes!

When you need to ensure a file isn’t changed under any circumstances in Windows, it’s time to make it read-only. Setting a file as read-only or write-protecting a file prevents any changes whatsoever. If you don’t know how to set files as read-only and need to, you’re in the right place!

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

Let’s get going.

Prerequisites

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

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

How to Make a File Read-Only (and Writable) in File Explorer

There are a few different ways to set a file as read-only but let’s start out this tutorial by first covering how to make it happen via Windows File Explorer.

The read-only attribute is stored as a metadata property rather than in the content of the file. The property can be seen through Windows File Explorer.

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

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

2. Right click the file and click Properties to open the file’s 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 set it as read-only, click Apply to assign the read-only attribute, and OK to close 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. Note the location of the Read-only attribute check box and the Apply button.

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

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

File attributes aren’t just exposed via File Explorer. You can also manipulate file attributes including the read-only attribute via the command line. To do so, you must use a command called attrib.

1. Open up a command prompt (cmd.exe).

2. Navigate to the folder the file is in.

cd c:\shared

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 can see below the readme.txt file does not have the read-only attribute enabled.

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

4. Now, run attrib to assign the read-only attribute. The +r parameter is key here. This parameter tells Windows to set the read-only attribute on the file.

attrib +r readme.txt

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

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

Similarly, to remove the read-only attribute use the -r parameter.

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

If you’re not a fan of the GUI or the command prompt or want to use have a little more ease of control, look no further than PowerShell! Let’s now learn how to make a file read-only 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’s currently enabled.

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

Using the Format-Table cmdlet is not necessary but by doing so, tells PowerShell to display only the two properties you’re interested in in an easy-to-view format (Name and Attributes)

3. Assign the read-only attribute by adding the ReadOnly attribute to the Attributes property as shown below.

## Capture the file object in the $file variable
 $file = Get-Item -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 Only Attribute

4. Now, view the Attributes property of the $file object and you’ll see that you’ve now applied the ReadOnly attribute to the file while maintaining any existing attributes that existed on the file beforehand. You can also run Get-Item again to view the same results.

How to make a file read-only (Viewing the Attribute Property)
How to make a file read-only (Viewing the Attribute Property)

To remove the ReadOnly attribute from the file, follow the exact same approach but this time set the value of the Attributes property to Normal e.g. $file.Attributes = "Normal".

Conclusion

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

Armed with this new knowledge of protecting files from being accidentally overwritten, do you have any files you’d like to protect?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!