# How do I catch this?

**URL:** <https://forums.powershell.org/t/how-do-i-catch-this/2651>\
**Category:** PowerShell Help\
**Created:** [June 4, 2014, 12:16am UTC](https://forums.powershell.org/t/how-do-i-catch-this/2651 "2014-06-04T00:16:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony-wainwright](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@tony-wainwright](https://forums.powershell.org/u/tony-wainwright)\
**Post date:** [June 4, 2014, 12:16am UTC](https://forums.powershell.org/t/how-do-i-catch-this/2651/1 "2014-06-04T00:16:52Z")

</div>

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`  
Thanks  
Tony

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 4, 2014, 12:19am UTC](https://forums.powershell.org/t/how-do-i-catch-this/2651/2 "2014-06-04T00:19:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tony-wainwright](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@tony-wainwright](https://forums.powershell.org/u/tony-wainwright)\
**Post date:** [June 4, 2014, 12:27am UTC](https://forums.powershell.org/t/how-do-i-catch-this/2651/3 "2014-06-04T00:27:18Z")

</div>

Thanks Don…it 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\Syst…es\WMI\RtBackup:String) [Get-ChildItem], Unaut
horizedAccessException
` - `FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [June 4, 2014, 12:36am UTC](https://forums.powershell.org/t/how-do-i-catch-this/2651/4 "2014-06-04T00:36:09Z")

</div>

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.
}
```

---

<div class="post-metadata">

**Author:** ![tony-wainwright](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@tony-wainwright](https://forums.powershell.org/u/tony-wainwright)\
**Post date:** [June 4, 2014, 12:41am UTC](https://forums.powershell.org/t/how-do-i-catch-this/2651/5 "2014-06-04T00:41:25Z")

</div>

Thanks, worked a treat.

---

<div class="post-metadata">

**Author:** ![martin-nielsen](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@martin-nielsen](https://forums.powershell.org/u/martin-nielsen)\
**Post date:** [June 4, 2014, 4:27am UTC](https://forums.powershell.org/t/how-do-i-catch-this/2651/6 "2014-06-04T04:27:18Z")

</div>

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
`

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:52pm UTC](https://forums.powershell.org/t/how-do-i-catch-this/2651/7 "2024-05-16T20:52:01Z")

</div>

Closed on May 16, 2024, 8:52pm UTC


