I’m writing a browser function for remote folders that gets the subfolder list recursively. The [IO.Directory]::GetDirectories() method stumbles upon protected folders like “System Volume Information” or “Documents and Settings” relusting in “Access is denied” error. Here’s a simplified example:
$Path = "\\SomeServerName\C$"
net use $path p@ssw0rd123 /user:SomeUserName
$DirList = [IO.Directory]::GetDirectories($Path)
Foreach ($Item in $DirList){
Write-Host "Current item:" $Item
#$ErrorActionPreference = 'SilentlyContinue'
$SubDirList = [IO.Directory]::GetDirectories($Item)
Write-Host "Subfolders:"
$SubDirList
}
Of course I can wrap that operation in a try/catch block or suppress $ErrorActionPreference temporarily but that’s a kludge. How can I than distinguish if a “real” error occurs that needs user’s attention?
Is there a way to filter out these system folders in the first place?