Gather Error Information while still running through Items

I’m needing to conduct a search of a user’s network drive, identifying all of the files that are older than X days. While writing a simple get-ItemChild script that will dive through a Recurse search of the drive I’m encountering folders that my administrative account doesn’t have access to. These network drives are decades old, and the organization of the drive and permissions is a mess. I’d like to capture a list of the folders that I don’t have access to, so that we can then go through and correct the permissions on the folder. The error is occurring at the time of the recursive gathering of the folders.

Is there a way to grab the name of the folder that I’m encountering access issues?

 

At C:\Temp\OldFiles.ps1:34 char:12 + $folders = get-childitem $dir -Directory -Force -Recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ReadError: (\\fileserver\user_sata\UserName\Favorites:String) [Get-ChildItem], IOException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand
 

You may collect all the errors using the -ErrorVariable parameter.
$folders = get-childitem $dir -Directory -Force -Recurse -ErrorVariable Errors
In the end $Errors will contain all the gci errors.

I had tried that before I posted here. Here is what I used for the test.

 

$folders = Get-ChildItem -Path $dir -Directory -Recurse -Exclude F* -ErrorAction SilentlyContinue -ErrorVariable ProcessError

If ($ProcessError) {Write-host “Error caught”; Write-host $ProcessError}

 

All that returns is a “An unexpected network error occurred.” message.

Is there an attribute of the Error that I can capture instead of the generic error message?

I’m new to Powershell. I found what I was looking for.

$ProcessError.CategoryInfo.TargetName