Home > PowerShell > PowerShell: Check if a file is read only or not

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
Categories: PowerShell
  1. Anton
    November 12, 2010 at 5:57 pm | #1

    Wrong command: set_isreadyonly, should be set_isreadonly.

  2. November 20, 2010 at 6:49 pm | #2

    I corrected. It’s a typo

  1. No trackbacks yet.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>