Trace an expression or command.
Syntax
Trace-Command [-command] string [-argumentList Object[]] [-name] string[]
[-option] PSTraceSourceOptions [-FilePath string]
[-debugger] [-PSHost] [-ListenerOption TraceOptions]
[-inputObject psobject]
[-force] [CommonParameters]
Trace-Command [-Expression] scriptblock [-Name] string[]
[-Option] PSTraceSourceOptions [-FilePath string]
[-Debugger] [-PSHost] [-ListenerOption TraceOptions]
[-InputObject psobject] [-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.
-ArgumentList Object[]
Parameters and parameter values for the command being traced.
This feature is especially useful for debugging dynamic parameters.
-Command string
A command that is being processed during the trace.
-Debugger
Send the trace output to the debugger (or in Visual Studio) This will
also select the default trace listener.
-Expression scriptblock
The expression to be processed during the trace.
Enclose the expression in curly braces {}
-FilePath string
Send the trace output to specified file. This will also select the
file trace listener.
-Force
Override restrictions that prevent the command from succeeding, apart
from security settings. e.g. override the read-only attribute on a file.
-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.
-ListenerOption TraceOptions
Add optional data to the prefix of each trace message in the output:
"None,LogicalOperationStack,DateTime,Timestamp,
ProcessId,ThreadId or Callstack"
-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".
-PassThru
Pass the object created by this cmdlet through the pipeline.
-PSHost
Send the trace output to the PowerShell host.
This will also select the PSHost trace listener.
CommonParameters:
-Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
-OutBuffer -OutVariable.
Trace-command works like Set-TraceSource except that it applies only to the specified command.
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*"
PS C:\> 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 Cmdlets:
Set-Tracesource - Trace a PowerShell component.
Get-Tracesource - Get components that are instrumented for tracing.