PowerShell: Debugging Tip
When I work on PnP PowerShell (https://github.com/pnp/powershell) I code either in VSCode or in Visual Studio as I write the cmdlets in C#. During development I need to debug the cmdlets I’m building so I need to attach the debugger to the PowerShell session where I run my cmdlets.
If there is one PowerShell window/tab running, then there is no problem. However… When I run multiple PowerShell sessions (no surprise: I most of the time do…) then which process do I attach to?
Fortunately there is an easy way to identiy the process PowerShell is using. Simply enter this in the PowerShell session:
$PID
It will return the current process ID, and now I can use this to select the correct PowerShell session.
Making it even easier would be if we simply show the ID of the process in the window title!
Let’s set that up.
Adding the Process ID of PowerShell to the Window Title
Edit your PowerShell Profile ($PROFILE
) (see PowerShell: Easy Navigation Between Folders for more details on $PROFILE
).
Add this line:
$Host.UI.RawUI.WindowTitle = "[PID: $PID] : PowerShell $($PSVersionTable.PSVersion)"
Save your profile and restart PowerShell. Now your title should show:
Now if you want to attach the debugger in VSCode or Visual Studio it is very easy to determine which process to attach to: