Powershell script to omit traversing access denied folders from a list of folders

powershell, powershell-2.0, powershell-3.0

Solution

You can set your error action preference per cmdlet like:

Get-ChildItem -recurse -ErrorAction SilentlyContinue | ? {$_.permission -match "read"}

Or you can set it per script using the System variable:

`$ErrorActionPreference = "SilentlyContinue"`

`Get-Help about_CommonParameters`

Problem

I was trying to do a script to list extract the folders and subfolders and the number of files for a particular path of directory. How to exclude the folders whose access is denied in the script? I used the `get-childitem` code snippet along with `where {$_.permission -match "read"`, I don't know if what I am trying is correct or not. I ended up with the following error:message: CategoryInfo : ReadError: (\support....-242\New folder:String) [Get-ChildItem], IOException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

Original source