Exploring PowerShell script debugging
PowerShell, both Windows PowerShell and PowerShell 7, contains some great debugging features. Using these features makes it easier to find and remove errors in your scripts. You can set breakpoints in a script – when you run your script, PowerShell stops execution at the breakpoint. For example, you can set a breakpoint to stop at a particular line, any time your script writes to a particular variable, or any time PowerShell calls a particular cmdlet.
When PowerShell encounters a breakpoint, it suspends processing and presents you with a debugging prompt, as you see in this recipe. You can then examine the results so far and run additional commands to ensure your script produces the output and results you expect. If your script adds a user to the AD and then performs an action on that user (adding the user to a group, for example), you could stop the script just after the Add-ADUser
command completes. At that point, you could use Get...