Using Debug and Verbose parameters in PowerShell

It’s one of those things you forget, and you can get a lot more output when running scripts that will help you find out why things are not working as you expected. This small blog post will show you how to get more verbose and debug information.

What are Debug and Verbose in PowerShell?

These options are part of the Common Parameters:

“The common parameters are a set of cmdlet parameters that you can use with any cmdlet. They’re implemented by PowerShell, not by the cmdlet developer, and they’re automatically available to any cmdlet.

You can use the common parameters with any cmdlet, but they might not have an effect on all cmdlets. For example, if a cmdlet doesn’t generate any verbose output, using the Verbose common parameter has no effect.

The common parameters are also available on advanced functions that use the CmdletBinding attribute or the Parameter attribute.

Several common parameters override system defaults or preferences that you set using the PowerShell preference variables. Unlike the preference variables, the common parameters affect only the commands in which they’re used.”

When should you use what?

You can use the -Verbose parameter in many cmdlets to show what it does during processing. If you are like me and don’t like staring at a blinking cursor, not knowing if there’s any progress, then you can use -Verbose to show you the progress.

Using the –Debug parameter in a cmdlet will show the same thing as the -Verbose parameter but will ask you to stop or continue each step instead of executing the whole command.

Examples

Verbose

I was talking to a former colleague about disabling the Power Management options of a Realtek network card; we tried the -SelectiveSuspend parameter, but nothing happened. But also no information about it. When running the same Disable-NetAdapterPowerManagement cmdlet but with -Verbose, it told us a lot more:

Another example of using -Verbose is that it shows what items were affected by running a cmdlet. For example, running this without it just gives you the prompt without telling you what and how many instances of notepad it killed. Running it with -Verbose will show you what PIDs and how many instances it killed:

Debug

Running the same Disable-NetAdapterPowerManagement cmdlet as above, but with -Debug will ask you to confirm steps before executing:

Running Get-Process | Stop-Process together with -Debug will ask you for every instance whether it should be killed:

Wrapping up

In the examples above, I showed you how the -Debug and -Verbose parameters can help you get more information and control while running cmdlets or scripts. This is beneficial when troubleshooting. Have a lovely weekend!