email

Tuesday, November 3, 2009

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:\temp\test.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:\temp\test.txt
$status.set_isreadyonly($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

No comments:

Post a Comment