# Script, piping and error handling

**URL:** <https://forums.powershell.org/t/script-piping-and-error-handling/2596>\
**Category:** PowerShell Help\
**Created:** [May 20, 2014, 10:00pm UTC](https://forums.powershell.org/t/script-piping-and-error-handling/2596 "2014-05-20T22:00:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul-bendall](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/paul-bendall/32/226_2.png) [@paul-bendall](https://forums.powershell.org/u/paul-bendall)\
**Post date:** [May 20, 2014, 10:00pm UTC](https://forums.powershell.org/t/script-piping-and-error-handling/2596/1 "2014-05-20T22:00:01Z")

</div>

Okay confesioon time, most of my scripts to date have been aimed at my use and don’t include error controls. I’m trying to stpe things up a little and write better scripts.

So without error handling I have something like:

Get-ADUser -LDAPFilter $LDAPUserFilter -ResultSetSize $null -Properties $ListofUserADProperties | %{  
$GN = $_.GivenName  
$SN = $_.Surname  
$CN = $_.cn  
$Mail = $_.mail  
$Addresses = $_.proxyAddresses  
$DN = $_.DistinguishedName  
}

In the past I’m sure I read that best practice is to process along the pipeline rather than return all results into an object?

Using Try / Catch is this still true?

The following doesn’t appear to be acceptable as the curly bracket closes the loop rather than the catch:  
Try {  
Get-ADUser -LDAPFilter $LDAPUserFilter -ResultSetSize $null -Properties $ListofUserADProperties | %{  
$GN = $_.GivenName  
$SN = $_.Surname  
$CN = $_.cn  
$Mail = $_.mail  
$Addresses = $_.proxyAddresses  
$DN = $_.DistinguishedName  
}  
}

Thanks

Paul

---

<div class="post-metadata">

**Author:** ![peter-jurgens-2](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@peter-jurgens-2](https://forums.powershell.org/u/peter-jurgens-2)\
**Post date:** [May 20, 2014, 11:44pm UTC](https://forums.powershell.org/t/script-piping-and-error-handling/2596/2 "2014-05-20T23:44:03Z")

</div>

Firstly it seems you haven’t included a catch statement in your script. The syntax for try/catch/finally is like this:

```
Try{}Catch{}Finally{}
```

You’re require to have at least the try and catch blocks.  
In the try block you will put your code that you want to catch errors for, and in the catch block you would enter the alternate code or output should the code in the try block fail with a terminating error.

The terminating error part is also important. If the script in your try block does not fail with a terminating error, try/catch will not “catch” the error and run the script in the catch block. For most cmdlefs you can add the -ErrorAction Stop parameter to the cmdlet in the try block.

---

<div class="post-metadata">

**Author:** ![mikefrobbins](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/mikefrobbins/32/272_2.png) [@mikefrobbins](https://forums.powershell.org/u/mikefrobbins)\
**Post date:** [May 21, 2014, 12:47am UTC](https://forums.powershell.org/t/script-piping-and-error-handling/2596/3 "2014-05-21T00:47:58Z")

</div>

Here’s an example of using a Try, Catch, Finally block to handle errors. As the previous person said. Try, Catch, Finally only handles terminating errors so you’ll need to use -ErrorAction Stop to turn non-terminating errors into terminating ones.

```auto

try {
    1/0
}
catch [DivideByZeroException] {
    Write-Warning -Message "Divide by zero exception"
}
catch [System.Net.WebException],[System.Exception] {
    Write-Warning -Message "Other exception"
}
finally {
    Write-Warning -Message "cleaning up ..."
}

```

Try is mandatory and either Catch or Finally is required, although you’ll almost always see a Catch block and Finally omitted. As a general rule, don’t wrap an entire script or function in Try, Catch, Finally, just use it where you think errors might occur. See the about\_try\_catch\_finally help topic in PowerShell for more information. I also have some examples of using error handling in PowerShell scripts and functions on my blog site. [Here’s one](http://mikefrobbins.com/2014/04/11/return-an-error-message-from-a-remote-computer-when-an-attempt-to-install-a-package-using-the-oneget-module-fails/) that has a couple of simple examples of using error handling.

---

<div class="post-metadata">

**Author:** ![paul-bendall](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/paul-bendall/32/226_2.png) [@paul-bendall](https://forums.powershell.org/u/paul-bendall)\
**Post date:** [May 21, 2014, 2:18am UTC](https://forums.powershell.org/t/script-piping-and-error-handling/2596/4 "2014-05-21T02:18:30Z")

</div>

Thanks both for the reply. I think I excluded too much from my snippet!

Is this best?

```auto

try {
    Get-ADUser -LDAPFilter $LDAPUserFilter -ResultSetSize $null -Properties $ListofUserADProperties | ForEach-Object {
        $GN = $_.GivenName 
        write-host $GN	
    }	
} catch {
    #Bad outcome
}

```

or, is this better

```auto

try {
    $ADUsers = Get-ADUser -LDAPFilter $LDAPUserFilter -ResultSetSize $null -Properties $ListofUserADProperties
}
Catch {
    #Something bad happened
}

ForEach($User in $ADUsers){
    write-host $User.GivenName    
}

```

---

<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/script-piping-and-error-handling/2596/5 "2024-05-16T20:52:02Z")

</div>

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


