SoFunction
Updated on 2025-04-08

Powershell tips: Cumulative record errors using variables

When you use GC to query files, you may miss quite a few errors when you do not have enough permissions. You can ignore these errors using "-ErrorAction SilentlyContinue".

There is a good way to get these errors.

We query all PS script files in the Windows directory, save its file in $PSScripts, and log the error log in the variable ErrorList:

Copy the codeThe code is as follows:

$PSScripts = Get-ChildItem -Path c:\windows -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue -ErrorVariable ErrorList

$ErrorList | ForEach-Object {
  Write-Warning ('Access denied: ' + $_.)
}

Supports all versions