Skip to content

Check and amend the read only attribute with Powershell

20/03/2017

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.

Advertisements

From → Powershell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: