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 7968615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:06:16+00:00 2026-06-04T07:06:16+00:00

Powershell is returning a 0 exit code, when an error has occurred, if called

  • 0

Powershell is returning a 0 exit code, when an error has occurred, if called with the -File argument. Which means my build is green when it shouldn’t be 🙁

For example:

(in wtf.ps1)

$ErrorActionPreference = "Stop";   
$null.split()

(cmd)

powershell -file c:\wtf.ps1  
You cannot call a method on a null-valued expression.
At C:\wtf.ps1:3 char:12
+ $null.split <<<< ()
    + CategoryInfo          : InvalidOperation: (split:String) [], ParentConta
   insErrorRecordException
    + FullyQualifiedErrorId : InvokeMethodOnNull


echo %errorlevel%  
0

powershell c:\wtf.ps1  
You cannot call a method on a null-valued expression.
At C:\wtf.ps1:3 char:12
+ $null.split <<<< ()
    + CategoryInfo          : InvalidOperation: (split:String) [], ParentConta
   insErrorRecordException
    + FullyQualifiedErrorId : InvokeMethodOnNull


echo %errorlevel%  
1

Any ideas?

(I’ve tried pretty much every idea from the first 2 pages of this: https://www.google.co.uk/search?q=powershell+file+argument+exit+code already)

  • 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-06-04T07:06:17+00:00Added an answer on June 4, 2026 at 7:06 am

    In the script, use the exit keyword with a number of your choice:

    exit 34
    

    Here’s the script I used to test this:

    ## D:\Scripts\Temp\exit.ps1 ##
    try{
        $null.split()
    }
    catch
    {
        exit 34
    }
    
    exit 2
    #############################
    
    # launch powershell from cmd 
    C:\> powershell -noprofile -file D:\Scripts\Temp\exit.ps1
    C:\>echo %errorlevel%
    34
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Powershell has two means for importing additional cmdlets into a session, Add-PsSnapIn and Import-Module.
Powershell provides the New-WebServiceProxy cmdlet which allows for a web service proxy object to
In Powershell I am defining a new PSDrive called test . But when I
I am having a PowerShell script which is walking a directory tree, and sometimes
When executing a Powershell script (in 2.0) using the -File command line switch, and
//powershell code $quiotent = divRemainderQuiotent ($a) ($b) ([ref] $remainder) # I need to pass
My PowerShell script file is located in C:/this-folder/that-folder/another-folder/powershell-file.ps1 . How do I get a
Is it possible to know the return value prepared by powershell before returning from
PowerShell 3.0 has several built-in NetAdapter cmdlets / functions. Is it possible to connect
I'm powershell newbie and I have a .csv file like this: Group1; User_A Group1;

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.