{"id":142765,"date":"2020-12-28T16:00:19","date_gmt":"2020-12-28T13:00:19","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-debug-a-powershell-script-cloudsavvy-it\/"},"modified":"2020-12-28T16:00:19","modified_gmt":"2020-12-28T13:00:19","slug":"how-to-debug-a-powershell-script-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-debug-a-powershell-script-cloudsavvy-it\/","title":{"rendered":"#How to Debug a PowerShell Script \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a41745b7c570\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a41745b7c570\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-debug-a-powershell-script-cloudsavvy-it\/#How_does_PowerShell_Debugging_Work\" >How does PowerShell Debugging Work?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-debug-a-powershell-script-cloudsavvy-it\/#PowerShell_Debugging_in_Visual_Studio_Code\" >PowerShell Debugging in Visual Studio Code<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-debug-a-powershell-script-cloudsavvy-it\/#Using_VS_Code_Debugging\" >Using VS Code Debugging<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-debug-a-powershell-script-cloudsavvy-it\/#Using_the_Command_Debugging_Commands\" >Using the Command Debugging Commands<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-debug-a-powershell-script-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Debug a PowerShell <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Script<\/a> \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4374\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/8fec593cd19e3b20a0773cafaaaaf848\/p\/uploads\/2020\/03\/23e4a5a4.png\" alt=\"Powershell logo\" width=\"1400\" height=\"578\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Any script development usually requires troubleshooting which usually results in one of two approaches. Either manually setting variables to values and outputting that data when running the script, or utilizing debugging to set breakpoints to inspect data before continuing or aborting. Built-in to PowerShell ISE and to Visual Studio Code is the ability to perform debugging.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_does_PowerShell_Debugging_Work\"><\/span>How does PowerShell Debugging Work?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Included with Windows PowerShell, the PowerShell ISE has been the traditional go-to environment to debug PowerShell scripts. With a built-in menu for debugging and a graphical representation of breakpoints, it is easy to get started. First, any script that you are debugging must be saved. Next, you will need to set breakpoints using one of three ways.<\/p>\n<ul>\n<li>On the focused line use the F9 key to set a breakpoint.<\/li>\n<li>Right-click next to a line and choose Toggle Breakpoint to set a breakpoint.<\/li>\n<li>Use the <code>Set-PSBreakPoint<\/code> specifying a line, variable, function, or matched text.<\/li>\n<\/ul>\n<p>Once you have a breakpoint set, you can simply start debugging by running the script with F5, or Debug \u2192 Run\/Continue. Once the script reaches the specified breakpoint, execution will be paused and you will need to take one of the following actions.<\/p>\n<ul>\n<li><strong>Step Over<\/strong> \u2013 Execute only the current statement, stopping before the next statement. If the current statement is a function or script, execute the entire call before stopping.<\/li>\n<li><strong>Step Into<\/strong> \u2013 Similar to Step Over, execute only the current statement, stopping before the next statement. Instead of executing the entire function or script, if called, step into the call.<\/li>\n<li><strong>Step Out<\/strong> \u2013 If within a function, step out and up one level if the function is nested. If in the main script execute until the end of the script or next breakpoint, whichever is first. Skipped statements are executed but not stepped through.<\/li>\n<\/ul>\n<p>When the script is stopped at a breakpoint, you can hover over a variable with the mouse cursor and see what the values of any given variable are. Additionally, you can use the command-line to output variables as well to see the current status. There are a few caveats to this. <code>$_<\/code>, <code>$Input<\/code>, <code>$MyInvocation<\/code>, <code>$PSBoundParameters<\/code>, and <code>$Args<\/code> will not work as they are automatic variables and not subject to debugging. One way to retrieve those values is to assign them to an inter<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>te variable that can then be set for a breakpoint.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8713\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/ab9a919e9fa5093756aed309d713523f\/p\/uploads\/2020\/12\/3ff9579d.png\" alt=\"\" width=\"1261\" height=\"703\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"PowerShell_Debugging_in_Visual_Studio_Code\"><\/span>PowerShell Debugging in Visual Studio Code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Very similar to how the PowerShell ISE works, Visual Studio Code adds several very useful debugging options. Additionally, VS Code allows you to easily debug with PowerShell 7.x while the ISE is traditionally limited to just Windows PowerShell. Using the same key-commands as the PowerShell ISE, you can quickly get started with debugging. Even better is that you now have the debugging pane within VS Code which consolidates much of the variable output and makes seeing a snapshot of all variable values in your code easier to grasp.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Using_VS_Code_Debugging\"><\/span>Using VS Code Debugging<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Just like the PowerShell ISE, you can use F5 to start an interactive debugging session. Unlike the ISE, VS Code offers a much better and more useful debugging experience. Not only do you get the traditional Debug console, indicated by the <code>[DBG]<\/code> prompt, but you have all of the known variables and their output in the left-hand debugging pane. To make navigating debug options better, the top debug bar makes stepping into, out, and over breakpoints very easy. Also, you are not required to save before running!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8714\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/99030faa818ba3cf03a199a089221e37\/p\/uploads\/2020\/12\/1a1166c7.png\" alt=\"\" width=\"1336\" height=\"776\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<blockquote><p><em>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.PowerShell\">PowerShell extension of VS Code<\/a> is required for this to work easily.<\/em><\/p>\n<\/blockquote>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_the_Command_Debugging_Commands\"><\/span>Using the Command Debugging Commands<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Although debugging strictly from the command-line is not the easiest operation to do at times, you can do this using the built-in debugging commands. There are a few built-in commands to make this easy.<\/p>\n<p>Using the versatile <code>Set-PSBreakPoint<\/code>, you can quickly set a breakpoint that once hit will drop you into the debugging command prompt. At that point, using the standard key-combinations you can quickly inspect variables and continue or break execution at that point.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Debugging does not need to be difficult and once you get started with this using any of the above methods, you will find it integral to your script development needs. Check out how you can get started with PowerShell Debugging today!\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong>\n<\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/8712\/how-to-debug-a-powershell-script\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Debug a PowerShell Script \u2013 CloudSavvy IT&#8221; Any script development usually requires troubleshooting which usually results in one of two approaches. Either manually setting variables to values and outputting that data when running the script, or utilizing debugging to set breakpoints to inspect data before continuing or aborting. Built-in to PowerShell ISE and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":142766,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/23e4a5a4.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-142765","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/142765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=142765"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/142765\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/142766"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=142765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=142765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=142765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}