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

Comments

  1. 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
    }
    }
    }

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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)

    ReplyDelete
  4. 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

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

    ReplyDelete

Post a Comment

Popular posts from this blog

A sharepoint list view of the current month

The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters

Export SharePoint 2010 List to Excel with PowerShell