Trace an expression or command
Syntax
Trace-Command [-name] string[] [-expression] scriptblock
[-option] PSTraceSourceOptions [-filePath string]
[-debugger] [-pSHost] [-listenerOption TraceOptions]
[-inputObject psobject] [-force] [CommonParameters]
Trace-Command [-name] string[] [-command] string
[-option] PSTraceSourceOptions [-filePath string]
[-debugger] [-pSHost] [-listenerOption TraceOptions]
[-inputObject psobject] [-argumentList Object[]]
[-force] [CommonParameters]
Key
-name string[]
Name of the trace source of each component to be traced.
Wildcards are permitted.
Use Get-TraceSource to find the trace sources on your computer.
-expression scriptblock
The expression to be processed during the trace.
Enclose the expression in curly braces {}
-option PSTraceSourceOptions
Type of events to trace, comma separated enclosed in quotes:
"None,Constructor,Dispose,Finalizer,Method,Property,Delegates,
Events,Exception,Lock,Error,Errors,Warning,Verbose,WriteLine,
Data,Scope,ExecutionFlow,Assert,All".
-filePath string
Send the trace output to specified file. This will also select the
file trace listener.
-debugger
Send the trace output to the debugger (or in Visual Studio) This will
also select the default trace listener.
-pSHost
Send the trace output to the PowerShell host.
This will also select the PSHost trace listener.
-listenerOption TraceOptions
Add optional data to the prefix of each trace message in the output:
"None,LogicalOperationStack,DateTime,Timestamp,
ProcessId,ThreadId or Callstack"
-inputObject psobject
An input to the expression that is being processed during the trace.
This can be a variable, or an object passed through the pipeline.
-argumentList Object[]
Parameters and parameter values for the command being traced.
This feature is especially useful for debugging dynamic parameters.
-passThru
Pass the object created by this cmdlet through the pipeline.
-force
Override restrictions that prevent the command from succeeding, apart
from security settings. e.g. override the read-only attribute on a file.
CommonParameters:
-Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable.
The following values for PSTraceSourceOptions are combinations of other values:
ExecutionFlow: "Constructor,Dispose,Finalizer,Method,Delegates,Events,Scope"
Data: "Constructor,Dispose,Finalizer,Property,Verbose,WriteLine"
Errors: "Error,Exception"
To specify multiple PSTraceSourceOptions, separate with commas, no spaces, and enclose in quotation marks.
Examples
Start a trace of the "get-process notepad" expression using the defaults: "All" for tracing and "None" for the listener option:
PS C:\>Trace-Command -name metadata,parameterbinding,cmdlet -expression {get -process notepad} -pshost
Trace the actions of the ParameterBinding operations of PowerShell while it processes a Get-Alias expression:
PS C:\>$a = "i*"
trace-command -name parameterbinding {get-alias $input} -pshost -inputobject $a
The -InputObject parameter passes the variable $a to the expression.
In effect, the command being processed during the trace is "get-alias -inputobject $a"
“Follow your instincts. That's where true wisdom manifests itself” - Oprah Winfrey
Related Powershell Commands:
Set-Tracesource - Trace a PowerShell component