{"id":80395,"date":"2016-11-04T11:59:56","date_gmt":"2016-11-04T18:59:56","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=80395"},"modified":"2019-02-18T09:10:22","modified_gmt":"2019-02-18T16:10:22","slug":"powertip-capture-error-code-of-a-powershell-session-in-linux-or-macos","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-capture-error-code-of-a-powershell-session-in-linux-or-macos\/","title":{"rendered":"PowerTip:  Capture error code of a PowerShell session in Linux or macOS"},"content":{"rendered":"<p><strong>Summary<\/strong>: Capture the status code of a PowerShell session in Linux or macOS.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> Could you show me how to run a PowerShell script in Linux or macOS and capture the status of whether that script succeeded or failed?<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/> No problem, my friend. Just run the PowerShell session, and start the script as in the following example. You can view the status in the <code>$?<\/code> built-in variable in Linux and macOS.<\/p>\n<p style=\"padding-left: 90px\"><code>powershell -file .\/samplescript.ps1<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>echo $?<\/code><\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\" alt=\"The Doctor\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Capture the status code of a PowerShell session in Linux or macOS. Could you show me how to run a PowerShell script in Linux or macOS and capture the status of whether that script succeeded or failed? No problem, my friend. Just run the PowerShell session, and start the script as in the following [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,639,641],"tags":[56,356,154,45],"class_list":["post-80395","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-powertip","category-windows-powershell","tag-guest-blogger","tag-powertip","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Capture the status code of a PowerShell session in Linux or macOS. Could you show me how to run a PowerShell script in Linux or macOS and capture the status of whether that script succeeded or failed? No problem, my friend. Just run the PowerShell session, and start the script as in the following [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=80395"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80395\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=80395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=80395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=80395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}