Set Read-Only in PowerShell

Here’s a fast PowerShell tip.  Need to mark a file as Read-Only? Use Get-ChildItem to get the file and then invoke the Set_IsReadOnly method.  This method needs a Boolean value (True or False) to indicate whether a file is ReadOnly. 

(Get-ChildItem e:\files\protected.txt).Set_IsReadOnly($True)

That’s all there is to it.  If you want to check if the file is readonly:

(Get-ChildItem e:\files\protected.txt).IsReadOnly

Will display True or False.

You could also use the set_Attributes method, but be sure to include all the current attributes if you are trying to add ReadOnly. Otherwise set_Attributes will erase the old attributes and use whatever you assign.

Technorati tags: , ,