≡ Menu

PowerShell: Check if a file is read only or not

This small powershell script checks if a given file is read-only or not.
 $status = Get-ChildItem c:temptest.txt
 If ($status.isreadonly)
 {
 Write-host -b yellow -f red “The file, $status.fullname is a read-only file”
 }
Else
{
Write-host -b yellow -f red “The file, $status.fullname is a read-only file”
}
If you want to make a file as readonly, use the below code
$status = Get-ChildItem c:temptest.txt
$status.set_isreadonly($true)
If ($status.isreadonly)
{
Write-host -b yellow -f red “The file, $status.fullname is a read-only file”
}
Else
{
Write-host -b yellow -f red “The file, $status.fullname is a read-only file”
}
If you want to remove the read-only a ttribute just change the parameter of set_isreadonly method to $false.
Happy Learning,
Sitaram Pamarthi

Comments on this entry are closed.

  • Anton November 12, 2010, 5:57 pm

    Wrong command: set_isreadyonly, should be set_isreadonly.

  • Sitaram Pamarthi November 20, 2010, 6:49 pm

    I corrected. It’s a typo