Home > Software engineering >  Check if user has read permissions on a directory (powershell)
Check if user has read permissions on a directory (powershell)

Time:12-21

I have found an answer on how to check whether the current user has read permissions on a file with powershell.

How can I do a similar check for directories? (Without having to install additional modules.)

CodePudding user response:

Don't bother with checking access control lists.

The proper way to check if you have read permissions on any object is plain and simple. Try to read it, and catch the "access denied" error.

try {
    Get-ChildItem "path\to\that\directory" -ErrorAction Stop
} catch [System.UnauthorizedAccessException] {
    Write-Host "$($_.TargetObject): Access denied"
} catch [System.Management.Automation.ItemNotFoundException] {
    Write-Host "$($_.TargetObject): Path not found"
}
  • Related