59

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. Thanks!

1
  • None of the answers get us back to "echo on" equivalent. Any update in the past 12 years? Jul 16, 2022 at 17:39

4 Answers 4

65

The following command will output each line of script to Write-Debug-

Set-PSDebug -Trace 1

From man Set-PSDebug

When the Trace parameter is set to 1, each line of script is traced as it is executed. When the parameter is set to 2, variable assignments, function calls, and script calls are also traced. If the Step parameter is specified, you are prompted before each line of the script is executed.

1
  • 3
    If you add this to a script that uses Param, make sure to place this line below the param-defining block or it can cause failures. Dec 12, 2018 at 21:12
7

Where I used echo on in CMD, I now use Write-Verbose and Set-PSDebug -Step instead. They are not the same, but they are more powerful if wielded skillfully.

2

Ugly:

PS > get-content foo.ps1|foreach-object{$_;invoke-expression "$_"}
$procs=get-process powershell
foreach($proc in $procs){$proc.processname}
powershell
PS > get-content foo.ps1
$procs=get-process powershell
foreach($proc in $procs){$proc.processname}
PS >

The problem with the above is that if you have multi-line commands like this:

foreach($proc in $procs){
  $proc.processname
}

The above will fail with my example above if that's placed in foo.ps1 with that structure...

1
  • 1
    Yeah, I keep finding half-solutions like this. I really think there isn't a "silver bullet" for what I want. Thanks for being creative with these solutions. I have a couple of options now and will have to decide where to go from here. Jan 21, 2010 at 2:26
2
help about_History 

Will tell you about all the commands and

Get-History [options]  

will return the full list for you to manipulate\display etc.

7
  • 2
    I found "set-psdebug -trace 1" which will show the commands, but a lot of extra "noise" I don't want. get-history doesn't output anything in a script. If it did I could stick it at the end of a script, but then the commands wouldn't be in-line (before the command output) and any exceptions would skip it (unless I catch it of course). It's still a good one to keep in mind... Any other ideas? Jan 12, 2010 at 23:55
  • Ah - do you mean that you want to capture the input and the output, or at least you want to echo the input commands immediately followed by corresponding output? One way to do that is to push the results into an array and then loop through the get-history results outputting the command followed by its output.
    – Helvick
    Jan 13, 2010 at 0:07
  • start-transcript might also do what you're looking for but it's going to be limited to the console text part of the action only.
    – Helvick
    Jan 13, 2010 at 0:15
  • You got it--the same way a batch file works. We have a bunch of batch files now and I'm looking at the feasibility of replacing those with PowerShell scripts. We have software for scheduling, maintaining output history, etc. Without the input command being echoed, it's harder to debug. I'm guessing pushing the results requires you do that on every command, make sure you catch exceptions, etc. so you don't miss anything. Another option, but not quite what I'm looking for. We may just have to pick one option and run with it. Jan 13, 2010 at 12:47
  • I tried start-transcript, but it only logged that it started/stopped, but nothing in between. I read somewhere it's meant for interactive commands. Jan 13, 2010 at 12:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .