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.
Table of Contents
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.
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
1. Open up a command prompt (cmd.exe).
2. Navigate to the folder the file is in.
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.
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
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.
Similarly, to remove the read-only attribute use the
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!
- 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
Format-Tablecmdlet 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 (
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")
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.
To remove the
ReadOnly attribute from the file, follow the exact same approach but this time set the value of the
Attributes property to
$file.Attributes = "Normal".
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?
More from Adam The Automator & Friends
Find out how many of your Active Directory users are using leaked passwords by running a free read-only scan with Specops Password Auditor.
Do more with PowerShell and transform your Code to Self Service with au2mator Self Service Portal
Why not write on a platform with an existing audience and share your knowledge with the world?