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.

Remove the read-only attribute from any files which have a file extension beginning “rs”.

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:

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.

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.



Written by Stephen Moon
email: stephen at

Hey! Did you enjoy reading this? If you did and would like an email when I add new content, just subscribe to my list. You can unsubscribe at any time.

Leave a Reply

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