PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

Geek Sync Live

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.

Latest Forum Posts

07-22-2016 - Start-Process
07-22-2016 - Compare-object strange behaviour
07-21-2016 - Rename many files in a folder with PowerShell

As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Debugging Other PowerShell Processes

PowerShell 5

Beginning with PowerShell 5.0, the PowerShell ISE can connect to other processes that run a PowerShell runspace, display the source code, and debug the foreign process. This is great news because now you can debug PowerShell code that runs hidden as scheduled task, or within an application such as SCCM.

Here are the basic steps:

# get the process (or process ID) of the
# process you want to debug
# (this assumes that another PowerShell console runs)
$p = Get-Process powershell | Select-Object -First 1

# connect to the process:
Enter-PSHostProcess -Process $p 

# connect to a runspace inside this process
# typically, ID 1 is correct, but there may be more
# use Get-Runspace to see all runspaces and their IDs
Debug-Runspace -Id 1

Provided that the connected process currently runs a PowerShell script, the PowerShell ISE invokes the debugger, shows the source code, and allows you to set breakpoints and step through the script as if it was a local script.

If the process is executing interactive content, no source code is shown. If the process is idle, nothing spectacular happens.

Twitter This Tip! ReTweet this Tip!

Copyright 2012 PowerShell.com. All rights reserved.