Using the Test-Path command in powershell, how can I tell the difference between a "folder doesn't exist" and "access denied"?
Powershell – Using Powershell and Test-Path, how can I tell the difference between a “folder doesn’t exist” and “access denied”
powershell
Related Topic
- Powershell – In PowerShell, how to define a function in a file and call it from the PowerShell commandline
- Powershell – get the latest created folder from a path using powershell
- Powershell – How to search a string in multiple files and return the names of files in Powershell
- Windows – Difference between $? and $LastExitCode in PowerShell
- Windows – PowerShell difference between Write-Host and Write-Output
- Powershell Invoke-WebRequest Fails with SSL/TLS Secure Channel
Best Answer
TL;DR: The good news is that
Test-Path
will often not return false even when you lack permissions (and when it doesn't, you'll get an exception instead of a simple$false
)In more depth, it depends on what you mean by access denied. Depending on what permissions you are looking to check, will depend on what PowerShell command will work for you.
For example, C:\System Volume Information is a folder that non-administrators have no permissions for.
Test-Path
returns true for this folder - it exists - even though you can't access it. On the other hand, runningGet-Child-Item
fails. So in this case, you would need to runIf however, you have read permissions but not write permissions, then you'll have to actually attempt to write to the file, or look at its security permissions and try to figure out what applies to the current user the script is running under: