Zulakis
Zulakis

Reputation: 8384

Check if user has read permissions on a directory (powershell)

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

Upvotes: 0

Views: 1801

Answers (1)

Tomalak
Tomalak

Reputation: 338228

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"
}

Upvotes: 3

Related Questions