Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 270789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:03:28+00:00 2026-05-12T00:03:28+00:00

PowerShell is a weird mix of .bat and .NET. In .bat, you check errorlevel

  • 0

PowerShell is a weird mix of .bat and .NET. In .bat, you check errorlevel and stderr output from commands. In .NET, you catch exceptions.

How do cmdlets return errors? Do they throw exceptions when they fail or do they set $? instead? Is this configurable?

I also assume that .NET functions I call in PowerShell will always throw exceptions and not get automatically caught by the shell and converted into errors. Is that correct?

Maybe what I really ought to ask is: what’s a good article that goes over all of this? Seems like a lot of engineers out there like me who have experience in cmd’s .bat and .NET both are wondering exactly how we ought to be doing things in this brave new world of Posh.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:03:29+00:00Added an answer on May 12, 2026 at 12:03 am

    For individual cmdlets, there is a parameter called -erroraction. The possible values are SilentlyContinue, Stop, Continue, or Inquire. You can also specify a global variable called $errorpreference to any of these options.

    In V1, you can use the trap key word. There is a pretty good, concise article that describes the key differences between traps and try/catch/finally syntax that was added in V2.

    Here is a quick example of using trap statements, the first is for a specif type of exception and the second is a generic catch all error trap

    trap {"Other terminating error trapped" }
    trap [System.Management.Automation.CommandNotFoundException] 
          {"Command error trapped"}
    1/$null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found a weird behavior using powershell. The output of a quoted object alone
As PowerShell has many SQL query-like cmdlets, is there a fast way to check
I am seeing some rather weird behavior with PowerShell, it looks like custom functions
I want to use Powershell to write some utilities, leveraging our own .NET components
I am running the following command: ([xml](new-object net.webclient).DownloadString( http://blogs.msdn.com/powershell/rss.aspx )).rss.channel.item | format-table title,link The
PowerShell 3.0 has several built-in NetAdapter cmdlets / functions. Is it possible to connect
PowerShell can call commandline batch files. PowerShell script output can be recorded with the
With powershell 2.0: write-output abcd >> mytext.txt returns: a nul b nul c nul
I have a powershell script that modifies transport rules. It works perfectly from powershell
I have a Powershell script that is loading a .NET assembly (.EXE in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.