Check and amend the read only attribute with Powershell

We can use the following to iterate through a series of folders and check the read only status of the files:

$Directories = "C:\drop\Test Complete\Dev\Global\Master\SeleniumTests"
gci -Recurse -Path ${Directories} | select fullname,isreadonly

It is not necessary to specify the list of directories as a variable but for my use it makes sense.
We use the Get-ChildItem and the -Recurse option to work through each file in each folder specified and their sub-folders. We then output the full filename path and read only status.

FullName                                                                                                                         isreadonly
--------                                                                                                                         ----------
C:\drop\Test Complete\Dev\Global\Master\SeleniumTests\ST_Bronn_358615_SingleSignOn_TestNG
C:\drop\Test Complete\Dev\Global\Master\SeleniumTests\ST_Bronn_360996_CreateSchoolsReport
C:\drop\Test Complete\Dev\Global\Master\SeleniumTests\SeleniumTests.tcUT                                                         True
C:\drop\Test Complete\Dev\Global\Master\SeleniumTests\ST_Bronn_358615_SingleSignOn_TestNG\ST_Bronn_358615_SingleSignOn_TestNG... False
C:\drop\Test Complete\Dev\Global\Master\SeleniumTests\ST_Bronn_360996_CreateSchoolsReport\ST_Bronn_360996_CreateSchoolsReport... False

As we can see in the above output, the first two lines display no attribute as these are folders and the next three lines are files with one having the read only attribute set to true.
The next thing we may want to do is to remove the read only attribute, to do that we can use the following:

$Directories = "C:\drop\Test Complete\Dev\Global\Master\SeleniumTests"
gci  -Recurse -Path ${Directories} | % { if($_.IsReadOnly){$_.IsReadOnly= $false} }

Here we pipe the results of the folder/file iteration into an if statement, if the IsReadOnly property is set for the file then the attribute is set to false.
Run the above followed by the first script to check the attribute and we see the following:

FullName                                                                                                                         isreadonly
--------                                                                                                                         ----------
C:\drop\Test Complete\Dev\Global\Master\SeleniumTests\ST_Bronn_358615_SingleSignOn_TestNG
C:\drop\Test Complete\Dev\Global\Master\SeleniumTests\ST_Bronn_360996_CreateSchoolsReport
C:\drop\Test Complete\Dev\Global\Master\SeleniumTests\SeleniumTests.tcUT                                                         False
C:\drop\Test Complete\Dev\Global\Master\SeleniumTests\ST_Bronn_358615_SingleSignOn_TestNG\ST_Bronn_358615_SingleSignOn_TestNG... False
C:\drop\Test Complete\Dev\Global\Master\SeleniumTests\ST_Bronn_360996_CreateSchoolsReport\ST_Bronn_360996_CreateSchoolsReport... False

This can be called on remote servers by saving as a Powershell script file and using the Invoke-Command while passing the -ComputerName.


Posted

in

by

Comments

One response to “Check and amend the read only attribute with Powershell”

  1. […] may be useful to combine the output from this script with the script to remove the read only file attribute in a previous post so that any exceptions are captured and can be […]

Leave a Reply

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