82

This is a duplicate of https://serverfault.com/questions/102098/powershell-script-showing-commands-run. I thought it would be more appropriate to ask this question here.

I am playing around with PowerShell scripts and they're working great. However, I am wondering if there is any way to also show all the commands that were run, just as if you were manually typing them in yourself. This would be similar to "echo on" in batch files. I looked at the PowerShell command-line arguments, the cmdlets, but I didn't find anything obvious.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Nelson Rothermel
  • 9,436
  • 8
  • 62
  • 81
  • I am a programmer, and I love PowerShell, but I think this is a non-programming question, and belongs on ServerFault. – Jay Bazuzi Jan 16 '10 at 20:31
  • 24
    I am a programmer and I love PowerShell and I think this question is right at home at stackoverflow. I use PowerShell as part of my programming toolkit. – DrFloyd5 Jan 24 '20 at 15:45

4 Answers4

83
Set-PSDebug -Trace 1
  • 0: Turn script tracing off.
  • 1: Trace script lines as they run.
  • 2: Trace script lines, variable assignments, function calls, and scripts.

For more info: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-psdebug?view=powershell-6

wisbucky
  • 33,218
  • 10
  • 150
  • 101
  • Adding an article that explains this more and gives examples, for posterity's viewing pleasure: https://devblogs.microsoft.com/scripting/tracing-the-execution-of-a-powershell-script/ – Ari Sweedler May 06 '22 at 20:41
  • 2
    This does not show *"all the commands that were run"*. Actually no commands are shown at all. – user7860670 May 17 '23 at 11:44
  • Useful, but unfortunately, it doesn't expand variables like bash `set -x` does. – RonJohn May 26 '23 at 02:28
13

Start-Transcript doesn't catch any exe output. That's a show stopper for me. I hate to say it but the best way I've found to do this is:

cmd /c powershell.exe -file c:\users\hillr\foo.ps1 > foo.log

This captures everything AFAICT.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 1
    The problem is you have to sprinkle this all over your script, wherever you call an EXE. And if you invoke someone else's script that calls an EXE, you have to modify their script. So yeah, not ideal. :-) – Keith Hill Jan 14 '10 at 18:12
  • Ah, Andy's link probably explains why I wasn't seeing much with start-transcript. I'll play around with it some more tomorrow, along with Keith's idea (and Richard's below). – Nelson Rothermel Jan 21 '10 at 02:29
  • [**updated link from 1st comment**](https://devblogs.microsoft.com/powershell/workaround-for-start-transcript-on-native-processes/) And this is what's in that article: `Start-Transcript; ipconfig | Out-Default; Stop-Transcript` – papo Dec 11 '19 at 22:47
  • 1
    But this will not include error stream, for that: `...2>&1 >> file.log` – papo Dec 11 '19 at 22:58
4

I added -verbose to desired commands. E.g.

Copy-Item c:\xxx d:\xxx -verbose
Roman O
  • 3,172
  • 30
  • 26
3
C:\workspaces\silverlight> start-transcript -?

NAME
    Start-Transcript
    
SYNOPSIS
    Creates a record of all or part of a Windows PowerShell session in a text file.
    
    
SYNTAX
    Start-Transcript [[-Path] <string>] [-Append] [-Force] [-NoClobber] [-Confirm] [-WhatIf] [<CommonParameters>]
    
    
DESCRIPTION
    The Start-Transcript cmdlet creates a record of all or part of a Windows PowerShell session in a text file. The transcript includes all command that the user
     types and all output that appears on the console.
    

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113408
    Stop-Transcript 

REMARKS
    To see the examples, type: "get-help Start-Transcript -examples".
    For more information, type: "get-help Start-Transcript -detailed".
    For technical information, type: "get-help Start-Transcript -full".

Note #1: it only records things written to the main console output stream, not Warning / Error / Debug.

Note #2: if you need to record native console applications, you'll need a slight workaround

unbob
  • 331
  • 3
  • 7
Richard Berg
  • 20,629
  • 2
  • 66
  • 86
  • 2
    I biffed this too - but Start-Transcript does capture the other streams (except for the progress stream). It just doesn't capture EXE output unless you use the Out-Default trick that Andy refers to everywhere you use an EXE. – Keith Hill Jan 14 '10 at 18:18