

This can be a useful bit of information to the developer, as it lets us know exactly which directory we are in.
Powershell script debugger full#
In this example I then show the file name, including the full path to the file. When developing a module or a large complex script with many functions, there may be times when you think your script is calling one function when it is actually calling another. You’d be surprised how useful this can be. On the first line I have the name of the function. It’s a simple bit of formatting, but it makes the provided information much easier to read. Also note I indented the remaining lines two spaces, and used periods so the colons could be aligned. This will let the word DEBUG appear on a line by itself.


Since this is a multiline string, I’m going to do the same thing I did with the Verbose messages and begin with a carriage return / line feed escape sequence ( `r`n ). We created a variable, $dbgMsg to hold the debugging message we wish to display. Starting at $($st.ToString('yyyy-MM-dd hh:mm:ss = Function.: $fnįile Length.: $("įinished at $($et.ToString('yyyy-MM-dd hh:mm:ss tt'))Įlapsed Time only modifications made are to the process block. If PowerShell detects the -Debug switch is passed in, it will display any messages from the Write-Debug cmdlet.įor this demo, we’ll reuse the Show-FileInfo from the previous blog post, Fun With PowerShell Write-Verbose.
Powershell script debugger code#
Within your code simply use the Write-Debug cmdlet. Just like the Verbose switch, the Debug switch is built in. Later we’ll see some more options when it comes to $DebugPreference, but for now let’s dig into the Write-Debug cmdlet. Even if Write-Debug is executed, no text is displayed.įor these demos to work, you need to ensure your $DebugPreference is set to Continue, at least to start with. If it is set to Silentl圜ontinue, the message is suppressed. This will display the message provided to the Write-Debug cmdlet, after doing so it will continue to the next line of code. (If it is set to something else then likely you previously ran a script which changed it). If you want to see the value currently there, just type $DebugPreference into your terminal window and hit ENTER.ĭepending on how your system is configured, this will usually be set to either Continue or Silentl圜ontinue. $DebugPreference – An Introductionīefore we start discussing the Write-Debug cmdlet, we need to cover a PowerShell built in variable, $DebugPreference. You can display the contents of any variable by highlighting it and using F8/F5. To run a snippet of code highlight the lines you want to execute, then in VSCode press F8 or in the IDE F5. The examples should work in PowerShell 5.1 in the PowerShell IDE, although they’ve not been tested there. In this article I’ll be using PowerShell Core, 7.1.4, and VSCode. We’ll take a deeper look at Debug momentarily, but first let me mention that for all of the examples we’ll display the code, then under it the result of our code. These messages are meant to assist the PowerShell developer in trouble shooting and debugging their code. The -Debug messages are meant to target the developer. In covering Write-Verbose, I mentioned verbose messages are typically targeted at the average user. In this post we’ll dive into its counterpart, the -Debug switch and its companion Write-Debug cmdlet. Whenever a PowerShell script is behaving in a way that is completely unexpected, one of the first things I do is put some code in place to help me figure out what portions of the script have and have not executed.In my previous post, Fun With PowerShell Write-Verbose, I introduced the use of the built in -Verbose switch. That being the case, I wanted to pass along a few of the techniques I use. This process isn't usually a big deal for short PowerShell scripts, but it can be tough if you are writing a long script. One of the most difficult things about PowerShell scripting is finding and fixing any bugs that exist within the script.
