Skip to content

Powershell: Add or remove the ‘Read Only’ attribute from files

G8th February 2016

C1 Comment

TPowerShell

Use Microsoft PowerShell to add or remove the ‘read only’ attribute from large batches of files in a directory.

Making all files ‘Read Only’

The process of making all files in a directory ‘read only’ is quite easy using Microsoft PowerShell. The simplest approach is:

$source="c:\path\to\files\*" #path to files
Set-ItemProperty -Path $source -Name IsReadOnly -Value $true

The file attribute changes to ‘read only’ because IsReadOnly is set to “true”.

Making files of type ‘Read Only’

If the aim is to make only certain file types ‘read only’, that can be achieved my specifying the file types in the code. In this example, only .jpg and .png files will become ‘read only’:

$source="c:\path\to\files\*" #path to files
Set-ItemProperty -Path $source -include @("*.png", "*.jpg") -Name IsReadOnly -Value $true

Removing the ‘Read Only’ attribute from files

To remove the ‘read only’ attribute from files, the above code can be altered so that IsReadOnly is set to “false”. The following code would remove the ‘read only’ attribute from .jpg and .png files:

$source="c:\path\to\files\*" #path to files
Set-ItemProperty -Path $source -include @("*.png", "*.jpg") -Name IsReadOnly -Value $true

Recursing

The following code will allow sub-directories to be recursed:

$source="c:\path\to\files\*" #path to files
Get-Childitem -Recurse -Path $source -include @("*.png", "*.jpg") | Set-ItemProperty -Name IsReadOnly -Value $true
   

Comments:

One response to “Powershell: Add or remove the ‘Read Only’ attribute from files”

  • Written by matt on 11 August 2017:

    should the $value be set to false when trying to remove the read only attribute? above you have it set to true in your example of removing it.

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.