Where to place $ErrorActionPreference = "Stop"

Welcome Forums General PowerShell Q&A Where to place $ErrorActionPreference = "Stop"

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

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #73064

    Participant
    Topics: 15
    Replies: 21
    Points: 72
    Rank: Member

    Hi,
    In my script I have used try and catch, what is the difference when I place $ErrorActionPreference = "Stop" inside the Try braces or if I place it in the start of the script before try braces.

    Thanks,
    Kishor

  • #73066

    Participant
    Topics: 1
    Replies: 1330
    Points: 1,674
    Helping Hand
    Rank: Community Hero

    The difference is the time you set the preference. In the start it will be valid for all command that come after it.

    BTW: You don't have to use $ErrorActionPreference. It's enough when you put the parameter -ErrorAction Stop to the cmdlet in the try block.

    • #73171

      Participant
      Topics: 15
      Replies: 21
      Points: 72
      Rank: Member

      Thankyou Olaf!

The topic ‘Where to place $ErrorActionPreference = "Stop"’ is closed to new replies.

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