Using PowerShell to Set Files to Read-Only
We’ve recently set up a monitoring system at work where if all goes well with a file-based database integrity check, logs are created, an email is sent saying so, and a backup is made. If it doesn’t go so swimmingly though, we need to make sure that the database is placed into a read-only state so that should someone come in the next morning before we do and use the application, we know they won’t corrupt the database any further before it has been checked by ourselves or the supplier. As ever, PowerShell is ready and waiting.
Here are some examples with descriptions about what is happening.
Set all files with a file extension beginning “rs” to read-only.
Set-ItemProperty -path "*.rs*" -name IsReadOnly $true |
Remove the read-only attribute from any files which have a file extension beginning “rs”.
Set-ItemProperty -path "*.rs*" -name IsReadOnly $false |
One extra parameter you can add if you are unsure about what might happen is the “-WhatIf” parameter. This is really helpful if you are unsure of what the effect would be should you do it. To illustrate this, I’ve set up a test folder with two files in it names a.rs1 and a.rs2, as follows:
PS F:\test> dir |
Now, if we run one of the commands earlier using this handy parameter, this is the kind of thing you can expect to see printed on the console.
PS F:\test> Set-ItemProperty -path "*.rs*" -name IsReadOnly $true -WhatIf |
Looking for something a bit more advanced? You want to see other examples of more information about this command at the MSDN site for it.
Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com