23 April 2012

Uncheck read only file for all sub folder with PowerShell

This time I'll introduce PowerShell code that disables the read only for all the files in sub folders (Recurse)


The code:

$Path = "your root folder"
$Files = Get-ChildItem $Path -Recurse
ForEach ($File in $Files)
{
   Write-Host "File: " 
$File "IsReadOnly: " $File.IsReadOnly
   if ($File.Attributes -ne "Directory" -and $File.Attributes -ne "Directory, Archive")
   {

      if ($File.IsReadOnly -eq $true )
      {
          try 

          {
               Set-ItemProperty -path $File.FullName -name IsReadOnly -value $false
               write-host "file:" $File.FullName "is now false read only"  -foregroundcolor "magenta"
          }
          catch 
[Exception] 
          {
               Write-Host "Error at file " $Path "\" $File
               Write-Host $_.Exception.Message
          }
      }
    }
}

Code should help you, especially you're working with the team system - which locks the files to ReadOnly



Yours,
Roi

7 comments:

Anonymous said...

It thorws runtime error. I have modified it. Find it below

$Path = "your root folder"
$Files = Get-ChildItem $Path -Recurse
ForEach ($File in $Files)
{
Write-Host file:$File IsReadOnly: $File.IsReadOnly
if ($File.Attributes -ne "Directory" -and $File.Attributes -ne "Directory, Archive")
{
try
{
Set-ItemProperty -Path $Path"\"$File -name IsReadOnly -value $false
}
catch
{
Write-Host "Error at file " $Path "\" $File
}
}
}

Roi Kolbinger said...

Thanks I fix it

venkatx5 said...

None of the above script works. 1st Script always throw terminator missing error. The script given by Anonymous throws file not found error when the folder has sub folders and files. Because the path generated will be always "Rootfolder\FileName". The Subfolder name is not specified which causes the error.

Roi Kolbinger said...

I fixed the readonly script

venkatx5 said...

Still the code has issue.
Couple of errors.
1. The Closing Double quotes missing in Line 5.
2. "\" Should be replaced "\""
3. finally {} block is missing (Powershell reports error)

venkatx5 said...

Simple 1 line MS Dos Command will do this.
Remove Read only : attrib -r "c:\folderpath\*.*" /s
Remove Hide only : attrib -h "c:\folderpath\*.*" /s
Remove Read Only / Hide : attrib -h -s "c:\folderpath\*.*" /s

Roi Kolbinger said...

Thanks... I fixed the code and showed it on the image

Post a Comment