How To Set And Remove The Read Only Attribute To Files With PowerShell
Think you can do better? Show us!
PowerShell can be used to add or remove the “Read Only” attribute for one or many files. In this Snip, Matt demonstrates a couple of ways to change the read-only attribute by using the Set-ItemProperty cmdlet and storing a file to a variable with Get-ChildItem to modify the object’s IsReadOnly property. Matt will also demonstrate how to change the read-only attribute for files in sub-directories recursively.
In this snip, I'm going to show you how to set and remove the read only attribute to files with PowerShell. There, a couple different ways to change the read only attribute of files so let's go ahead and take a look at some examples. 1st I'm going to run a script. I created that will create some files and directories for me to use the script also sets a handful of these files to read only both of these PS1 files will be available on the techsnips, GitHub repository. Now that I've run my setup script. You can see that I have a series of folders now in each of these folders has 10 text files within them. The first file I'm going to work with is this test file dot TXT. When I right click, and look at the properties. You can see that the read only attribute is currently not checked off meaning this file is not read only. Now let's jump into the examples, the first thing I'm going to do is run get child item on the test file. This will output a few of the properties for that file but not the read only property. So let's pipe that now to select star. You can now see that there is a property called, is read only and it is currently set defaults, which we confirmed by looking at the file properties. Now, for the first method. I'm going to run get child item on the test file and store that in a variable called dollar sign source. Next I'm going to use the set item property command to change the is read only property to true. To do this, I'm going to pass the dollar source variable to the path parameter and for the name parameter. It is looking for the property. We want to set so I will pass it that is read only property. And finally is going to expect a value for the property, so I'm passing dollar sign true to the value parameter. Now, if I run get child item again to look at the properties. You can see that is read only is now set to true. Let's go confirm that in Explorer. When I right click on the file and click properties read only is now checked off the second method is going to be very similar, but it's going to require much less typing. First, let's go ahead and run get child item on the test file and store it in the dollar source variable. Now let's see what happens when we run dollar source dot is read only. You can see that is read only is set to true because we set it to true in the previous example in order to reset the read only property to false. All we need to do is run dollar source dot is read only equals dollar sign false. Now, when we run get child item on that file you will see that is read only is now set back to default finally let's go over how to set the read only property recursively in all subfolders. 1st I'm going to run get child item against the path of my root demo folder. And this time, I'm going to specify the recurse parameter. This is how we recursively get the files from within all the subfolders under the root demo folder, then I'm piping that too? Where is read only equals true? This will recursively return all the files that I have marked as read only. Now let's go ahead and store those files in a variable called dollar sign source. There are 2 options next. 1st. We can run set item property. For path will use dollar source dot full name, which includes the full path of the files. For name will specify is read only again and for value will use faults the other option, which is a little shorter as you can see is to just pipe dollar source to set item property with the same values for name and value. Both will have the same effect, so it's just a matter of preference. Now let's rerun get child item to display any files where read only is true. It returns no results because we set all the read only files defaults in the previous command. And that is how to set and remove the read only attribute to files with PowerShell.