Powershell: Add or remove the ‘Read Only’ attribute from files
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”
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.