Get PSCallStack - PowerShell


Overview

Get-PSCallStack provides detailed information about the current PowerShell call stack, including the active function or script, its parameters, and the chain of parent functions that led to its execution. It helps troubleshoot and analyze the flow of control in PowerShell scripts and functions.

Syntax

Get-PSCallStack [-Depth <Int32>] [-ListModules] [-ListFunctions] [-ListScriptFiles] [-ListExtensions] [-ShowFullCallChain] [-ListLocals] [-ListArgs] [-ListParameters] [-IncludeExceptionDetails] [-IncludeTrace] [-IncludeHelpInfo] [-IgnoreHidden] [-Verbose] [-Debug] [-ErrorAction {Stop | Continue | SilentlyContinue | Inquire | Ignore}] [-ErrorVariable <String>] [-OutVariable <String>]

Options/Flags

  • -Depth: Specifies the number of frames to include in the call stack. Default: 10.
  • -ListModules: Lists the modules that are loaded in the current session and their respective versions.
  • -ListFunctions: Lists the functions that have been defined within the scope of the current session.
  • -ListScriptFiles: Lists the script files that have been executed or imported during the current session.
  • -ListExtensions: Lists the PowerShell extensions that are loaded in the current session.
  • -ShowFullCallChain: Displays the complete call chain from the root script to the currently executing function.
  • -ListLocals: Lists the local variables that are defined within the scope of the current function or script.
  • -ListArgs: Lists the arguments that have been passed to the current function or script.
  • -ListParameters: Lists the parameters that are defined by the current function or script.
  • -IncludeExceptionDetails: Includes detailed exception information in the output if an exception has occurred.
  • -IncludeTrace: Includes the output of the -Trace parameter in the PSCallstack object.
  • -IncludeHelpInfo: Includes the synopsis and other help information for each function in the call stack.
  • -IgnoreHidden: Excludes hidden functions and modules from the output.
  • -Verbose: Writes detailed progress and debugging information to the console.
  • -Debug: Writes more detailed debugging information to the console.
  • -ErrorAction: Specifies the action to take when an error occurs.
  • -ErrorVariable: Stores error messages in a specified variable.
  • -OutVariable: Stores the output in a specified variable.

Examples

Simple call stack: Display the current call stack:

Get-PSCallStack

Deep call stack: Display 20 frames of the call stack:

Get-PSCallStack -Depth 20

List modules: Display all loaded modules:

Get-PSCallStack -ListModules

Full call chain: Display the full call chain from the root script:

Get-PSCallStack -ShowFullCallChain

Common Issues

  • Error: “Get-PSCallStack is not a recognized command.”
    • Solution: Ensure that you are running PowerShell version 5.1 or later.
  • Error: “Cannot find function.”
    • Solution: Verify that the function you are trying to trace has been defined and imported into the current session.

Integration

With -OutVariable: Store the call stack object in a variable for further analysis:

$callStack = Get-PSCallStack

With -Trace: Combine Get-PSCallStack with -Trace to trace the execution of a script or function:

Get-PSCallStack -Trace -OutVariable result | Out-File callStack.txt
  • Get-Command: Lists cmdlets, functions, aliases, and scripts available in the current session.
  • Get-Module: Lists loaded PowerShell modules.
  • Trace-Command: Traces the execution of a specified command or script.