How do I catch this?

Welcome Forums General PowerShell Q&A How do I catch this?

This topic contains 5 replies, has 4 voices, and was last updated by

4 years, 8 months ago.

  • Author
  • #15886

    Points: 0
    Rank: Member

    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

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

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

  • #15888

    Points: 0
    Rank: Member

    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

    Points: 0
    Rank: Member

    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

    Points: 0
    Rank: Member

    Thanks, worked a treat.

  • #15897

    Points: 0
    Rank: Member

    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

The topic ‘How do I catch this?’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort