2019-03-05 15:45:46 -05:00
# Debugging in PowerShell Command-line
2016-07-12 18:56:36 -07:00
2020-05-28 01:59:47 +01:00
As we know, we can debug PowerShell code via GUI tools like [Visual Studio Code ](https://docs.microsoft.com/powershell/scripting/dev-cross-plat/vscode/using-vscode#debugging-with-visual-studio-code ). In addition, we can
2019-03-05 15:45:46 -05:00
directly perform debugging within the PowerShell command-line session by using the PowerShell debugger cmdlets. This document demonstrates how to use the cmdlets for the PowerShell command-line debugging. We will cover the following topics:
setting a debug breakpoint on a line of code and on a variable.
2016-07-12 18:56:36 -07:00
Let's use the following code snippet as our sample script.
2019-03-05 15:45:46 -05:00
```powershell
2016-07-12 18:56:36 -07:00
# Convert Fahrenheit to Celsius
function ConvertFahrenheitToCelsius([double ] $fahrenheit )
{
$celsius = $fahrenheit - 32
$celsius = $celsius / 1.8
$celsius
}
$fahrenheit = Read-Host 'Input a temperature in Fahrenheit'
$result =[ int ]( ConvertFahrenheitToCelsius ( $fahrenheit ))
Write-Host " $result Celsius"
```
2020-05-28 09:29:56 +01:00
## Setting a Breakpoint on a Line
2016-07-12 18:56:36 -07:00
2016-08-01 11:21:55 -07:00
- Open a [PowerShell editor ](README.md#powershell-editor )
2016-07-14 17:50:56 -07:00
- Save the above code snippet to a file. For example, "test.ps1"
2016-07-13 19:18:29 -07:00
- Go to your command-line PowerShell
2016-07-12 18:56:36 -07:00
- Clear existing breakpoints if any
2019-03-05 15:45:46 -05:00
```powershell
2016-07-13 19:18:29 -07:00
PS / home / jen / debug > Get-PSBreakpoint | Remove-PSBreakpoint
2019-03-05 15:45:46 -05:00
```
2016-07-14 17:50:56 -07:00
- Use **Set-PSBreakpoint** cmdlet to set a debug breakpoint. In this case, we will set it to line 5
2016-07-12 18:56:36 -07:00
2019-03-05 15:45:46 -05:00
```powershell
2016-07-13 19:18:29 -07:00
PS / home / jen / debug > Set-PSBreakpoint -Line 5 -Script ./ test . ps1
2016-07-12 18:56:36 -07:00
ID Script Line Command Variable Action
-- ------ ---- ------- -------- ------
2016-07-13 19:18:29 -07:00
0 test . ps1 5
2016-07-12 18:56:36 -07:00
```
2019-03-05 15:45:46 -05:00
2016-07-14 17:50:56 -07:00
- Run the script "test.ps1". As we have set a breakpoint, it is expected the program will break into the debugger at the line 5.
2016-07-12 18:56:36 -07:00
2019-03-05 15:45:46 -05:00
```powershell
2016-07-12 18:56:36 -07:00
2016-07-13 19:18:29 -07:00
PS / home / jen / debug > ./ test . ps1
2016-07-12 18:56:36 -07:00
Input a temperature in Fahrenheit : 80
2016-07-13 19:18:29 -07:00
Hit Line breakpoint on '/home/jen/debug/test.ps1:5'
2016-07-12 18:56:36 -07:00
2016-07-13 19:18:29 -07:00
At / home / jen / debug / test . ps1 : 5 char : 1
2016-07-12 18:56:36 -07:00
+ $celsius = $celsius / 1.8
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >>
2016-07-12 18:56:36 -07:00
```
2016-08-01 11:21:55 -07:00
- The PowerShell prompt now has the prefix ** [DBG]:** as you may have noticed. This means
2016-07-14 17:50:56 -07:00
we have entered into the debug mode. To watch the variables like $celsius, simply type ** $celsius** as below.
- To exit from the debugging, type **q**
- To get help for the debugging commands, simply type ** ?**. The following is an example of debugging output.
2016-07-12 18:56:36 -07:00
```PowerShell
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> $celsius
2016-07-12 18:56:36 -07:00
48
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> $fahrenheit
2016-07-12 18:56:36 -07:00
80
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> ?
2016-07-12 18:56:36 -07:00
s , stepInto Single step ( step into function s , scripts , etc .)
v , stepOver Step to next statement ( step over function s , scripts , etc .)
o , stepOut Step out of the current function , script , etc .
c , continue Continue operation
q , quit Stop operation and exit the debugger
d , detach Continue operation and detach the debugger .
k , Get-PSCallStack Display call stack
l , list List source code for the current script .
Use "list" to start from the current line , "list <m>"
to start from line < m >, and "list <m> <n>" to list < n >
lines starting from line < m >
< enter > Repeat last command if it was stepInto , stepOver or list
?, h displays this help message .
For instructions about how to customize your debugger prompt , type "help about_prompt" .
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> s
At PS / home / jen / debug / test . ps1 : 6 char : 1
2016-07-12 18:56:36 -07:00
+ $celsius
+ ~~~~~~~~
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> $celsius
2016-07-12 18:56:36 -07:00
26.6666666666667
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> $fahrenheit
2016-07-12 18:56:36 -07:00
80
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> q
PS / home / jen / debug >
2016-07-12 18:56:36 -07:00
```
2020-05-28 09:29:56 +01:00
## Setting a Breakpoint on a Variable
2016-07-14 17:50:56 -07:00
- Clear existing breakpoints if there are any
2016-07-12 18:56:36 -07:00
2019-03-05 15:45:46 -05:00
```powershell
2016-07-13 19:18:29 -07:00
PS / home / jen / debug > Get-PSBreakpoint | Remove-PSBreakpoint
2016-07-12 18:56:36 -07:00
```
2019-03-05 15:45:46 -05:00
2016-07-14 17:50:56 -07:00
- Use ** Set-PSBreakpoint ** cmdlet to set a debug breakpoint . In this case , we set it to line 5
2016-07-12 18:56:36 -07:00
2019-03-05 15:45:46 -05:00
``` powershell
2016-07-12 18:56:36 -07:00
2016-07-13 19:18:29 -07:00
PS / home / jen / debug > Set-PSBreakpoint -Variable "celsius" -Mode write -Script ./ test . ps1
2016-07-12 18:56:36 -07:00
```
2016-07-14 17:50:56 -07:00
- Run the script "test.ps1"
2016-07-12 18:56:36 -07:00
Once hit the debug breakpoint, we can type **l** to list the source code that debugger is currently executing. As we can see line 3 has an asterisk at the front, meaning that's the line the program is currently executing and broke into the debugger as illustrated below.
2016-07-14 17:50:56 -07:00
- Type **q** to exit from the debugging mode. The following is an example of debugging output.
2016-07-12 18:56:36 -07:00
2019-03-05 15:45:46 -05:00
```powershell
./ test . ps1
2016-07-12 18:56:36 -07:00
Input a temperature in Fahrenheit : 80
2016-07-13 19:18:29 -07:00
Hit Variable breakpoint on '/home/jen/debug/test.ps1:$celsius' ( Write access )
2016-07-12 18:56:36 -07:00
2016-07-13 19:18:29 -07:00
At / home / jen / debug / test . ps1 : 3 char : 1
2016-07-12 18:56:36 -07:00
+ $celsius = $fahrenheit - 32
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> l
2016-07-12 18:56:36 -07:00
1 : function ConvertFahrenheitToCelsius([double ] $fahrenheit )
2 : {
3 : * $celsius = $fahrenheit - 32
4 : $celsius = $celsius / 1.8
5 : $celsius
6 : }
7 :
8 : $fahrenheit = Read-Host 'Input a temperature in Fahrenheit'
9 : $result =[ int ]( ConvertFahrenheitToCelsius ( $fahrenheit ))
10 : Write-Host " $result Celsius"
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> $celsius
2016-07-12 18:56:36 -07:00
48
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> v
At / home / jen / debug / test . ps1 : 4 char : 1
2016-07-12 18:56:36 -07:00
+ $celsius = $celsius / 1.8
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> v
Hit Variable breakpoint on '/home/jen/debug/test.ps1:$celsius' ( Write access )
2016-07-12 18:56:36 -07:00
2016-07-13 19:18:29 -07:00
At / home / jen / debug / test . ps1 : 4 char : 1
2016-07-12 18:56:36 -07:00
+ $celsius = $celsius / 1.8
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> $celsius
2016-07-12 18:56:36 -07:00
26.6666666666667
2016-07-13 19:18:29 -07:00
[ DBG ] : PS / home / jen / debug >> q
PS / home / jen / debug >
2016-07-12 18:56:36 -07:00
```
2016-07-14 17:50:56 -07:00
Now you know the basics of the PowerShell debugging from PowerShell command-line. For further learning, read the following articles.
2016-07-12 18:56:36 -07:00
2019-03-05 15:45:46 -05:00
## More Reading
2016-07-12 18:56:36 -07:00
2020-05-15 04:29:43 +01:00
- [about_Debuggers ](https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_debuggers )
2016-07-12 18:56:36 -07:00
- [PowerShell Debugging ](https://blogs.technet.microsoft.com/heyscriptingguy/tag/debugging/ )