PowerShell Genie

Trace-Command

Use Trace-Command to trace PowerShell's internal operations while running an expression, showing how it resolved what you asked for.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Trace-Command

Syntax

1PS> Trace-Command [-Name] <String[]> [-Expression] <ScriptBlock> [-Option <PSTraceSourceOptions>] [-ListenerOption <TraceOptions>] [-FilePath <String>] [-PSHost] [-Debugger] [<CommonParameters>]
See the command anatomy

Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

Trace-Command [-Name] <String[]> [-Expression] <ScriptBlock> [-Option <PSTraceSourceOptions>] [-ListenerOption <TraceOptions>] [-FilePath <String>] [-PSHost] [-Debugger] [<CommonParameters>]
  1. Stage 1Trace-Command-Name -Expression -Option -ListenerOption -FilePath -PSHost -Debugger

    Try it

    Windows PowerShell

    PowerShell example · browser support is limited.

    Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

    Interactive simulation · fictional data

    Examples

    Trace-Command -Name ParameterBinding

    1PS> Trace-Command -Name ParameterBinding -Expression { Get-Service -Name Spooler } -PSHost
    Show reference output

    Sample output · fictional data, shaped like the real thing

    DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Get-Service]
    DEBUG: ParameterBinding Information: 0 : BIND arg [Spooler] to parameter [Name]
    
    Status  Name     DisplayName
    ------  ----     -----------
    Running Spooler  Print Spooler

    Trace-Command -Name ParameterBinding (2)

    1PS> Trace-Command -Name ParameterBinding -Expression { # do work here } -FilePath C:\Temp\trace.txt
    Show reference output

    Sample output · fictional data, shaped like the real thing

    (No host output. Parameter-binding trace records were written to C:\Temp\trace.txt.)

    Select first 5

    1PS> Get-TraceSource | Select-Object -First 5 Name # the available trace sources
    Show reference output

    Sample output · fictional data, shaped like the real thing

    Name
    ----
    ParameterBinding
    CommandDiscovery
    TypeConversion
    Metadata
    Serialization

    Parameters

    Parameter definitions:

    • -Name
    • -Expression
    • -Option
    • -ListenerOption
    • -FilePath
    • -PSHost
    • -Debugger

    Trace-Command on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use Trace-Command when you need to trace PowerShell's internal operations while running an expression, showing how it resolved what you asked for. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

    Common error and fix: An unknown trace-source name produces no useful investigation. Inspect Get-TraceSource and select the source relevant to binding or command discovery before collecting a large trace. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

    See also: Write-Debug. Trace-Command observes selected engine trace sources; Write-Debug emits messages written by your code. Parameter binding traces answer different questions from application diagnostics. Read the linked command before substituting it; similar names do not imply identical inputs or output.

    Trace-Command reference on Microsoft Learn

    Recap

    Trace-Command Use Trace-Command to trace PowerShell's internal operations while running an expression, showing how it resolved what you asked for.

    Next: Get-Command
    Genie guideTrace-Command

    Use Trace-Command to trace PowerShell's internal operations while running an expression, showing how it resolved what you asked for. The console below is pre-loaded — run it, then change a value and run it again.