Files in Windows can have a read-only attribute set on them to prevent modification. Luckily for us, we can toggle this attribute on and off at will for all of our file management automation scripts with PowerShell.

To demonstrate this behavior, let's first create a few files in a new folder. We'll use these files to toggle the read-only attribute on and off. I'll call the folder 4SysOps.

$folderPath = 'C:\4SysOps'
New-Item -Path $folderPath -ItemType Directory -Force

Once I have created the folder, I'll then create a single file inside of it using the New-Item command.

New-Item -Path $folderPath\TestFile.txt -ItemType File -Force

Now that I have a file to demonstrate with, I'll first read the read-only property to see what the default value is. You can see below that the file has an IsReadOnly property. This property is available on all files and has a value of True or False depending on whether the file is read-only or not. By default, it looks like creating a file sets the IsReadOnly property to False.

PS> Get-ItemProperty -Path $folderPath\TestFile.txt | Select-Object IsReadOnly

IsReadOnly
----------
     False

To prove that it's not read-only, let's modify it by adding some text to it.

Set-Content -Path $folderPath\TestFile.txt -Value 'this changes the file'

The Set-Content cmdlet adds text to text files. If this file were set to read-only, we would have received an error.

Now we have a verified file that is not read-only, let's change the IsReadOnly attribute to True. To do this, we use the Set-ItemProperty command with the property name of IsReadOnly and set the Value to $true.

Set-ItemProperty -Path $folderPath\TestFile.txt -Name IsReadOnly -Value $true

Doing so produces no output, but we can confirm it's set to read-only now by using Get-ItemProperty and checking on the IsReadOnly property again.

Get-ItemProperty -Path $folderPath\TestFile.txt | Select-Object IsReadOnly
Checking the read only attribute

Checking the read only attribute

Now you can see the IsReadOnly property is set to True. We can confirm this by trying to change the file in some way again using Set-Content.

Attempting to change a read only file

Attempting to change a read only file

If you'd rather not use the Set-ItemProperty command to change the read-only attribute, you can also directly set the IsReadOnly property on the file object to either $true or $false. This method gets the same task done but is a little cleaner yet a little less obvious.

To use this method, we first have to get a file object using the Get-Item command.

$file = Get-Item -Path $folderPath\TestFile.txt

Once we have the file object, we can then directly set the IsReadOnly property to either $true or $false.

$file.IsReadOnly = $false

We can now look at the IsReadOnly property again, but this time we'll use dot notation instead.

$file.IsReadOnly
False

Referencing the IsReadOnly property directly or via the commands does the same thing.

Just like with anything else in PowerShell, once you have the technique down, you can also recursively change the read-only attribute. For example, if we had multiple files in our 4SysOps folder, we could query them all with the Get-ChildItem command and change them all in one shot!

Subscribe to 4sysops newsletter!

$setRoAttributeTo = $true
Get-ChildItem -Path $folderPath | foreach {
    Set-ItemProperty -Path $folderPath\TestFile.txt -Name IsReadOnly -Value $setRoAttributeTo
}

The snippet above would change all read-only attributes on all files in the $folderPath to True.

+3
avatar
3 Comments
  1. Miroslav 3 years ago

    one liner:   (Get-Item C:\Temp\).Attributes = "ReadOnly"

    +6
    avatar
  2. Rahul Padhy 3 years ago

    Hi...

    I was trying to verify the Inheritance property in Windows. So, I'm creating a folder having a specified number of files with random content, using Powershell. After that, I'm seeing that the Read-Only property of the folder is in an indeterminate state and the constituent files don't have the Read-Only property set - all this is expected.

    But, when I'm trying to set the attribute of a folder as 'Read-Only' using Powershell, I'm noticing that neither the folder nor the constituent files are having their 'Read-Only' attribute set. The commands I'm using are as follows :-

    PS C:\rp> $rp = mkdir C:\rp\rahul1234

    PS C:\rp> $rp.Attributes
    Directory

    PS C:\rp> $rp.Attributes = "ReadOnly"

    PS C:\rp> $rp.Attributes
    ReadOnly, Directory

    Can anyone please tell me as to how I set the attributes for a folder (Read-Only / Hidden / Archived) using Powershell scripting ?

    0

    • @Rahul

      Read-only attribute applies only to files, not folders. The thing is, when you do set read-only on a folder with Properties GUI it will ask you if you want to populate this to files in that folder.

      But that does not work via Powershell. You will need to get all items inside your folders and set the attribute directly on files. This simple one liner works

      Get-ChildItem d:\test -Recurse | % {$_.attributes = "readonly"}

      If you need more help I suggest to open a topic in Powershell forum.

      Cheers Leos

      +5
      avataravatar

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account