{"id":112,"date":"2014-05-22T12:49:45","date_gmt":"2014-05-22T10:49:45","guid":{"rendered":"http:\/\/www.lambiek.eu\/blog\/?p=112"},"modified":"2014-11-28T18:54:44","modified_gmt":"2014-11-28T17:54:44","slug":"windows-powershell-return-exit-code-of-script","status":"publish","type":"post","link":"https:\/\/www.lambiek.eu\/blog\/hint\/windows-powershell-return-exit-code-of-script\/","title":{"rendered":"Windows Powershell return exit code of script"},"content":{"rendered":"<p>If you want powershell to return the correct\/specified exit code of a script run it as follows:<\/p>\n<p>Content of test.ps1:<\/p>\n<pre class=\"lang:default decode:true \" >\r\nWrite-Host \"Hello World!\"\r\nExit 3\r\n<\/pre>\n<pre class=\"lang:default decode:true \" >\r\npowershell -NonInteractive -NoProfile -Command \".\\test.ps1; Exit $LastExitCode\"\r\n<\/pre>\n<p>If you need to parse this through cmd.exe or start.exe encapsulate it as follows:<\/p>\n<pre class=\"lang:default decode:true \" >\r\ncmd \/c \"echo.|powershell -NonInteractive -NoProfile -Command ^\".\\test.ps1; Exit $LastExitCode^\"\"\r\necho %ERRORLEVEL%\r\n<\/pre>\n<p>or using start.exe<\/p>\n<pre class=\"lang:default decode:true \" >\r\nstart \/wait powershell -WindowStyle Hidden -NonInteractive -NoProfile -Command \".\\test.ps1; Exit $LastExitCode\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want powershell to return the correct\/specified exit code of a script run it as follows: Content of test.ps1: Write-Host &#8220;Hello World!&#8221; Exit 3 powershell -NonInteractive -NoProfile -Command &#8220;.\\test.ps1; Exit $LastExitCode&#8221; If you need to parse this through cmd.exe or start.exe encapsulate it as follows: cmd \/c &#8220;echo.|powershell -NonInteractive -NoProfile -Command ^&#8221;.\\test.ps1; Exit $LastExitCode^&#8221;&#8221; &#8230; <span class=\"more\"><a class=\"more-link\" href=\"https:\/\/www.lambiek.eu\/blog\/hint\/windows-powershell-return-exit-code-of-script\/\">[Read more&#8230;]<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[12,8],"_links":{"self":[{"href":"https:\/\/www.lambiek.eu\/blog\/wp-json\/wp\/v2\/posts\/112"}],"collection":[{"href":"https:\/\/www.lambiek.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lambiek.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lambiek.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lambiek.eu\/blog\/wp-json\/wp\/v2\/comments?post=112"}],"version-history":[{"count":2,"href":"https:\/\/www.lambiek.eu\/blog\/wp-json\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":184,"href":"https:\/\/www.lambiek.eu\/blog\/wp-json\/wp\/v2\/posts\/112\/revisions\/184"}],"wp:attachment":[{"href":"https:\/\/www.lambiek.eu\/blog\/wp-json\/wp\/v2\/media?parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lambiek.eu\/blog\/wp-json\/wp\/v2\/categories?post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lambiek.eu\/blog\/wp-json\/wp\/v2\/tags?post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}