How do I catch this?

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Martin Nielsen Martin Nielsen 3 years ago.

  • Author
  • #15886
    Profile photo of Tony Wainwright
    Tony Wainwright

    I'm trying to modify a script that check the size of folders in a particular path, but get an error message when it hits a folder of zero bytes. Is there a way to use a Try Catch to suppress the error message?

    Code snippet
    $size = (Get-ChildItem $_.FullName -Recurse |
    WHERE {-not $_PSIsContainer} |
    Measure-Object -Sum Length).Sum

    Error Message
    Measure-Object : The property "Length" cannot be found in the input for any objects.
    At line:64 char:13
    + Measure-Object -Sum Length).Sum
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand


  • #15887
    Profile photo of Don Jones
    Don Jones

    You've got a typo. You're missing a period after the $_. That's why it's including folders.

  • #15888
    Profile photo of Tony Wainwright
    Tony Wainwright

    Thanks was actually a huge typo. The error I'm trying to catch is permission denied:
    Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\WMI\RtBackup' is denied.
    At T:\Scripts\Folders\Get-DirSize.ps1:62 char:18
    + $size = (Get-ChildItem $_.FullName -Recurse |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (C:\Windows\\WMI\RtBackup:String) [Get-ChildItem], Unaut
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

  • #15889
    Profile photo of Dave Wyatt
    Dave Wyatt

    That's a non-terminating error. I would tend to use a combination of -ErrorAction SilentlyContinue (to keep the red text off the screen) and -ErrorVariable (to allow your script to handle the error later):

    $size = (Get-ChildItem $_.FullName -Recurse -ErrorAction SilentlyContinue -ErrorVariable gciErrors |
                WHERE {-not $_.PSIsContainer} |
                Measure-Object -Sum Length).Sum
    foreach ($errorRecord in $gciErrors)
        # Do something with the error record.
  • #15890
    Profile photo of Tony Wainwright
    Tony Wainwright

    Thanks, worked a treat.

  • #15897
    Profile photo of Martin Nielsen
    Martin Nielsen

    In your case it might not be important to handle the error right away, but cases where it is you can explicitly tell the Cmdlet to throw a terminating Exception and trigger a try/catch by using -ErrorAction Stop, or by modifying the global $ErrorActionPreference variable.

    try {
    Get-ChildItem 'C:\' -Recurse -ErrorAction Stop
    catch [System.UnauthorizedAccessException] {
    "Unauthorized Access Exception"
    catch {
    "Unhandled Exception"

    If you want to handle specific errors, you can always look at the red error output to see what kind of Exception you're dealing with, for example:

    Get-ChildItem : Access to the path 'C:\Users\Public\Documents\Hyper-V' is denied.
    At line:1 char:1
    + Get-ChildItem C:\ -Recurse
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (C:\Users\Public\Documents\Hyper-V:String) [Get-ChildItem], UnauthorizedAccessException ## This is your Exception type
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

You must be logged in to reply to this topic.