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

  • SEARCH
  • Home
  • 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 6178233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:25:22+00:00 2026-05-24T00:25:22+00:00

I seem unable to catch an exception thrown by Start-Service . Here is my

  • 0

I seem unable to catch an exception thrown by Start-Service. Here is my code:

try
{
    start-service "SomeUnStartableService"
}
catch [Microsoft.PowerShell.Commands.ServiceCommandException]
{
    write-host "got here"
}

When I run this, the exception is thrown but not caught:

*Service 'SomeUnStartableService' start failed.
At line:3 char:18
+     start-service <<<<  "SomeUnStartableService"
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
    + FullyQualifiedErrorId : StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand*

$ErrorActionPreference is set to Stop, so this shouldn’t be the problem.

When I alter my code to catch [Exception], the exception is caught and “got here” is printed.

Does start-service throw a ServiceCommandException or something else? It looks as though it is but I cannot catch it!

— Edit —

Ideally I could write the following, and throw an exception if start-service did not throw an exception, and only catch an exception thrown by start-service:

try
{
    start-service "SomeUnStartableService"
    throw (new-object Exception("service started when expected not to start"))
}
catch [Microsoft.PowerShell.Commands.ServiceCommandException]
{
    write-host "got here"
}
  • 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-24T00:25:23+00:00Added an answer on May 24, 2026 at 12:25 am

    I usually don’t limit the catch-phrase but handle the exceptions with logical tests within the catch-block:

    try
    {
      start-service "SomeUnStartableService" -ea Stop
    }
    catch
    {
       if ( $error[0].Exception -match "Microsoft.PowerShell.Commands.ServiceCommandException")
       {
          #do this
       }
       else
       {
          #do that
       }
    }
    

    Maybe not as clean, and may result in huge catch blocks. But if it works… 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Excuse me if my question may seem inappropriate but i was unable to find
Seem to run into a service endpoint not found problem when trying to get
Is it possible to catch the exception when a referenced .dll cannot be found?
I delved into C# but came across a problem that I seem unable to
I have searched & searched but I seem unable to find a way to
Does anyone have experience with Sparse ? I seem unable to find any documentation,
Now that I have installed Silverlight 3, I seem unable to build a Silverlight
Ok, I've searched quite a bit, but seem unable to find an answer or
I have a simple requirement I seem unable to meet: I have a product
Currently with WMI's Win32_Process, I seem unable to detect if a batch file is

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.