Logging Folders with Access Denied Errors

by Feb 4, 2015

All Versions

When you browse the file system with Get-ChildItem, you may stumble across folders that you have no access to view. If you’d like to keep a log of all the folders that raised errors, try this approach:

$result = Get-ChildItem -Path c:\Windows -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue -ErrorVariable abcd

Write-Warning 'Unable to access these folders:'
Write-Warning ($abcd.TargetObject -join "`r`n") 

The trick is to hide all errors (-ErrorAction SilentlyContinue) but save the errors to a variable (-ErrorVariable abcd).

Twitter This Tip! ReTweet this Tip!