{"id":78793,"date":"2026-07-13T13:55:27","date_gmt":"2026-07-13T13:55:27","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78793"},"modified":"2026-07-13T13:55:27","modified_gmt":"2026-07-13T13:55:27","slug":"write-error","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78793","title":{"rendered":"Write-Error"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><code>Write-Error<\/code> in PowerShell<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Write-Error<\/code> writes an error message to PowerShell\u2019s <strong>error stream<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By default, it creates a <strong>non-terminating error<\/strong>. That means PowerShell displays the error, but usually continues with the next command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Write-Error \"The file could not be found.\"\n\nWrite-Output \"The script continued.\"\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Typical result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Write-Error: The file could not be found.\nThe script continued.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Make it terminating<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-ErrorAction Stop\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Write-Error \"The file could not be found.\" -ErrorAction Stop\n\nWrite-Output \"This line will not run.\"\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now the error stops normal execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use with <code>try\/catch<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Write-Error \"The server is unavailable.\" -ErrorAction Stop\n}\ncatch {\n    Write-Warning \"The error was caught.\"\n    Write-Warning $_.Exception.Message\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Possible output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>WARNING: The error was caught.\nWARNING: The server is unavailable.\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Without <code>-ErrorAction Stop<\/code>, the <code>catch<\/code> block normally does not run because <code>Write-Error<\/code> is non-terminating by default.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Inside a function<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>function Get-LabFile {\n    param(\n        &#91;string]$Path\n    )\n\n    if (-not (Test-Path -Path $Path)) {\n        Write-Error \"The file '$Path' does not exist.\"\n        return\n    }\n\n    Get-Content -Path $Path\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Call:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-LabFile -Path \".\\missing.txt\"\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Error details<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Write-Error<\/code> can provide more structured information:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Write-Error `\n    -Message \"The configuration file is missing.\" `\n    -Category ObjectNotFound `\n    -ErrorId \"ConfigFileMissing\" `\n    -TargetObject \".\\config.json\"\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This creates an error record containing:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Message<\/li>\n\n\n\n<li>Error category<\/li>\n\n\n\n<li>Error ID<\/li>\n\n\n\n<li>Target object<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><code>$Error<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Errors written by <code>Write-Error<\/code> are normally stored in the automatic <code>$Error<\/code> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Write-Error \"Testing error storage\"\n\n$Error&#91;0]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>$Error[0]<\/code> is the most recent error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><code>Write-Error<\/code> versus <code>throw<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Write-Error \"A problem occurred\"\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Non-terminating by default<\/li>\n\n\n\n<li>Usually continues<\/li>\n\n\n\n<li>Can be changed with <code>-ErrorAction Stop<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>throw \"A serious problem occurred\"\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Terminating<\/li>\n\n\n\n<li>Stops execution immediately unless caught<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><code>Write-Error<\/code> versus other output commands<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Command<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>Write-Output<\/code><\/td><td>Sends normal data to the success pipeline<\/td><\/tr><tr><td><code>Write-Warning<\/code><\/td><td>Displays a warning<\/td><\/tr><tr><td><code>Write-Verbose<\/code><\/td><td>Displays detailed information when <code>-Verbose<\/code> is used<\/td><\/tr><tr><td><code>Write-Error<\/code><\/td><td>Writes an error record<\/td><\/tr><tr><td><code>throw<\/code><\/td><td>Creates a terminating exception<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">A useful rule:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Use <code>Write-Error<\/code> when an operation has failed but the caller may decide whether execution should continue. Use <code>throw<\/code> when the operation cannot safely continue.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Write-Error in PowerShell Write-Error writes an error message to PowerShell\u2019s error stream. By default, it creates a non-terminating error. That means PowerShell displays the error, but usually continues with the next command. Typical result: Make it terminating Use: Example: Now the error stops normal execution. Use with try\/catch Possible output: Without -ErrorAction Stop, the catch &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=78793\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1981],"tags":[],"class_list":["post-78793","post","type-post","status-publish","format-standard","hentry","category-power-shell","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":78791,"url":"http:\/\/bangla.sitestree.com\/?p=78791","url_meta":{"origin":78793,"position":0},"title":"Terminating error vs non-terminating error","author":"Sayed","date":"July 13, 2026","format":false,"excerpt":"Terminating error vs non-terminating error in PowerShell Non-terminating error A non-terminating error reports a problem but allows PowerShell to continue running the remaining commands. Example: Get-Content -Path \".\\MissingFile.txt\" Write-Output \"The script continued.\" Possible output: Get-Content : Cannot find path '.\\MissingFile.txt' because it does not exist. The script continued. The file-reading\u2026","rel":"","context":"In &quot;Power Shell&quot;","block_context":{"text":"Power Shell","link":"http:\/\/bangla.sitestree.com\/?cat=1981"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78663,"url":"http:\/\/bangla.sitestree.com\/?p=78663","url_meta":{"origin":78793,"position":1},"title":"write-output vs write-host","author":"Sayed","date":"May 24, 2026","format":false,"excerpt":"Write-Output vs Write-Host in PowerShell FeatureWrite-OutputWrite-HostSends data to pipeline\u2705 Yes\u274c NoCan be stored in variable\u2705 Yes\u274c Usually noCan be redirected to file\u2705 Yes\u274c Not normally usefulUsed for script output\u2705 Recommended\u26a0\ufe0f Mainly for display messagesSupports formatting\/colorLimited\u2705 Good for colors 1. Write-Output Write-Output sends data to the PowerShell pipeline. Write-Output \"Hello PowerShell\"\u2026","rel":"","context":"In &quot;Power Shell&quot;","block_context":{"text":"Power Shell","link":"http:\/\/bangla.sitestree.com\/?cat=1981"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":65656,"url":"http:\/\/bangla.sitestree.com\/?p=65656","url_meta":{"origin":78793,"position":2},"title":"Debug and Trace classes in .Net #Misc .Net","author":"Author-Check- Article-or-Video","date":"July 10, 2021","format":false,"excerpt":"Brought from our old-site (yes, pretty old short-note): http:\/\/salearningschool.com\/displayArticle.php?table=Articles&articleID=636&title=Debug%20and%20Trace%20Classes%20in%20.Net Debug and Trace classes in .Net You can always debug your applications line by line. However, when the applications get very bigger\/larger, debugging line by line may not be an efficient way for finding errors in code (well, you can first\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":69926,"url":"http:\/\/bangla.sitestree.com\/?p=69926","url_meta":{"origin":78793,"position":3},"title":"Debug and Trace Classes in .Net #18","author":"Author-Check- Article-or-Video","date":"August 22, 2021","format":false,"excerpt":"Debug and Trace classes in .Net You can always debug your applications line by line. However, when the applications get very bigger\/larger, debugging line by line may not be an efficient way for finding errors in code. Debug and Trace classes are handy in such cases. Using Debug and Trace\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8469,"url":"http:\/\/bangla.sitestree.com\/?p=8469","url_meta":{"origin":78793,"position":4},"title":"\u09aa\u09bf\u098f\u0987\u099a\u09aa\u09bf \u09a4\u09cd\u09b0\u09c1\u099f\u09bf \u09aa\u09b0\u09bf\u099a\u09be\u09b2\u09a8\u09be\u09b0 \u09ac\u09cd\u09af\u09ac\u09b8\u09cd\u09a5\u09be (PHP Error Handling)","author":"Author-Check- Article-or-Video","date":"March 24, 2015","format":false,"excerpt":"\u09aa\u09bf\u098f\u0987\u099a\u09aa\u09bf \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae\u09bf\u0982 \u098f \u098f\u0995\u099f\u09bf Default Error Handling built-in \u09a5\u09be\u0995\u09c7 \u09af\u09be \u09b8\u09cd\u09ac\u09df\u0982\u0995\u09cd\u09b0\u09bf\u09df \u09ad\u09be\u09ac\u09c7 \u09af\u09c7 \u09ab\u09be\u0987\u09b2\u09c7 \u09ad\u09c1\u09b2 \u09b0\u09df\u09c7\u099b\u09c7 \u09b8\u09c7\u0987 \u09ab\u09be\u0987\u09b2\u09c7\u09b0 \u09a8\u09be\u09ae, \u09af\u09c7 \u09b2\u09be\u0987\u09a8 \u098f \u09ad\u09c1\u09b2 \u09b9\u09df\u09c7\u099b\u09c7 \u09a4\u09be\u09b0 \u09a8\u09ae\u09cd\u09ac\u09b0 \u098f\u09ac\u0982 \u0995\u09bf \u09a7\u09b0\u09a8\u09c7\u09b0 \u09ad\u09c1\u09b2 \u09b9\u09df\u09c7\u099b\u09c7 \u09a4\u09be \u09aa\u09cd\u09b0\u09a6\u09b0\u09cd\u09b6\u09a8 \u0995\u09b0\u09c7 \u09a5\u09be\u0995\u09c7 \u098f\u09ac\u0982 \u09ac\u09cd\u09b0\u09be\u0989\u09b8\u09be\u09b0\u09c7 \u098f\u0995\u099f\u09bf error message \u09aa\u09cd\u09b0\u09c7\u09b0\u09a8 \u0995\u09b0\u09c7\u0964 \u0986\u09ae\u09b0\u09be \u0989\u0995\u09cd\u09a4 error message \u09a6\u09c7\u0996\u09c7 \u0996\u09c1\u09ac \u09b8\u09b9\u099c\u09c7 \u0986\u09ae\u09be\u09a6\u09c7\u09b0\u2026","rel":"","context":"In &quot;\u09aa\u09bf \u098f\u0987\u099a \u09aa\u09bf \u099f\u09bf\u0989\u099f\u09cb\u09b0\u09bf\u09df\u09be\u09b2 \u0964 PHP tutorial&quot;","block_context":{"text":"\u09aa\u09bf \u098f\u0987\u099a \u09aa\u09bf \u099f\u09bf\u0989\u099f\u09cb\u09b0\u09bf\u09df\u09be\u09b2 \u0964 PHP tutorial","link":"http:\/\/bangla.sitestree.com\/?cat=172"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78719,"url":"http:\/\/bangla.sitestree.com\/?p=78719","url_meta":{"origin":78793,"position":5},"title":"CmdletBinding","author":"Sayed","date":"June 27, 2026","format":false,"excerpt":"Ref: AI Tools\/ChatGPT [CmdletBinding()] turns a normal PowerShell function into an advanced function. That means the function behaves more like a real PowerShell cmdlet. Basic example Normal function: function Install-Office { param( [string]$Version ) Write-Host \"Installing Office $Version\" } Advanced function: function Install-Office { [CmdletBinding()] param( [string]$Version ) Write-Host \"Installing\u2026","rel":"","context":"In &quot;Power Shell&quot;","block_context":{"text":"Power Shell","link":"http:\/\/bangla.sitestree.com\/?cat=1981"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78793"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78793\/revisions"}],"predecessor-version":[{"id":78794,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78793\/revisions\/78794"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78793"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}