{"id":9001,"date":"2012-06-19T00:01:00","date_gmt":"2012-06-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/06\/19\/use-powershell-to-find-and-change-read-only-files\/"},"modified":"2012-06-19T00:01:00","modified_gmt":"2012-06-19T00:01:00","slug":"use-powershell-to-find-and-change-read-only-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-find-and-change-read-only-files\/","title":{"rendered":"Use PowerShell to Find and Change Read-Only Files"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to find read-only Microsoft Excel files and to change them to read-write.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I have a problem that hopefully you can resolve. We recently restored a bunch of files from tape, and for some reason they all became marked as read-only. Our Help Desk has been inundated with calls from people who open a Microsoft Excel spreadsheet, make a bunch of changes to it, and then are prompted to save it with a different name. This is a HUGE problem because we have hundreds of spreadsheets that are saved in different places and used on a daily basis by dozens of different departments. For example, we have one spreadsheet with lots of special formulas that the production team updates at night, and the plant manager uses each morning to calculate our cost-per-unit for our plant report to corporate headquarters. His spreadsheet uses input from all the departments to make the calculations. If one spreadsheet changes its name or its path, the whole thing breaks. I simply must figure out a way to find all the Microsoft Excel spreadsheets and change them from being read-only. It might be my job otherwise.<\/p>\n<p>&mdash;RS<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello RS,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Well, it always seems to take longer to get back into the swing of things after being out of the office. If I spend three days away, it seems to take me five days to get back on track. I did not even look at any <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a> email this past week while I was at Microsoft TechEd in Orlando. So now, I am trying to catch up on those messages. RS, I am sorry you are having such a terrible time. I can certainly help you get things back on line, but for a long-term solution you should really be looking at SharePoint because it is designed to do what you have sort of cobbled together. As you have seen, linking spreadsheets like that can be a bit fragile.<\/p>\n<p>The first thing you need to do is to find all of the Microsoft Excel files in your system. To do this, you will use the <b>Get-ChildItem<\/b> cmdlet and use the <i>Include <\/i>parameter to allow you to search for all XLS and XLSX types of files. You will also need to use the <i>Recurse <\/i>switched parameter to search through the folder. The following command uses the <b>GCI<\/b> alias for the <b>Get-ChildItem<\/b> cmdlet.<\/p>\n<p style=\"padding-left: 30px\">gci -Include *.xls, *.xlsx -Recurse<\/p>\n<p>If you change your working directory to the location that contains the files, you will not need a <i>Path <\/i>parameter. This technique is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3782.hsg-6-19-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3782.hsg-6-19-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Because you more than likely have multiple directories to search, you can supply them to the cmdlet as an array. To do this, use the &shy;<i>Path <\/i>parameter as follows.<\/p>\n<p style=\"padding-left: 30px\">gci -Include *.xls, *.xlsx -Recurse -Path c:\\test, c:\\fso<\/p>\n<p>The command and the output associated with the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4762.hsg-6-19-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4762.hsg-6-19-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>To determine if a file is read-only, you check the <b>IsReadOnly<\/b><i> <\/i>property. The following command finds all of the Microsoft Excel documents in multiple folders, returns the complete path, and tells whether the file is read-only.<\/p>\n<p style=\"padding-left: 30px\">gci -Include *.xls, *.xlsx -Recurse -Path c:\\test, c:\\fso | select fullname,isreadonly<\/p>\n<p>The command and the output associated with the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6320.hsg-6-19-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6320.hsg-6-19-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>You now have two choices. First, you can determine if the file is read-only. If it is, you can set it to read-write. Or, the easier way, you can simply make all of the files in the folder read-write. The net result is the same. Obviously, the second option is the easier code to write. For the remaining examples, I will only use the C:\\test directory. In the following example, I use the <b>% <\/b>alias for the <b>Foreach-Object<\/b> cmdlet. I use the <b>If<\/b><i> <\/i>statement to determine if the file is read-only. If it is, I change it to read-write by setting the <b>IsReadOnly<\/b><i> <\/i>property to <b>$false<\/b>. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">gci -Include *.xls, *.xlsx -Recurse | % { if($_.IsReadOnly){$_.IsReadOnly= $false} }<\/p>\n<p>The command and the output associated with the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7827.hsg-6-19-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7827.hsg-6-19-12-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>It is much easier to simply change all the Microsoft Excel documents from read-only. Because I am in my test folder, I first change everything to read-only by using the following command.<\/p>\n<p style=\"padding-left: 30px\">gci -Include *.xls, *.xlsx -Recurse | % { $_.isreadonly = $true }<\/p>\n<p>I then change them back by setting the <b>IsReadOnly<\/b><i> <\/i>property on each file to <b>$false<\/b> as shown here.<\/p>\n<p style=\"padding-left: 30px\">gci -Include *.xls, *.xlsx -Recurse | % { $_.isreadonly = $false }<\/p>\n<p>The following image shows the commands and the associated output.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8407.hsg-6-19-12-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8407.hsg-6-19-12-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>RS, that is all there is to changing read-only files with Windows PowerShell. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to find read-only Microsoft Excel files and to change them to read-write. &nbsp;Hey, Scripting Guy! I have a problem that hopefully you can resolve. We recently restored a bunch of files from tape, and for some reason they all became marked as read-only. Our [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,3,12,45],"class_list":["post-9001","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to find read-only Microsoft Excel files and to change them to read-write. &nbsp;Hey, Scripting Guy! I have a problem that hopefully you can resolve. We recently restored a bunch of files from tape, and for some reason they all became marked as read-only. Our [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=9001"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9001\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=9001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=9001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=9001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}