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!
-
None of the answers get us back to "echo on" equivalent. Any update in the past 12 years? – Daniel Williams Jul 16 '22 at 17:39
4 Answers
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.

- 953
- 1
- 7
- 11
-
3If 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. – kayleeFrye_onDeck Dec 12 '18 at 21:12
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.

- 683
- 5
- 14
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.

- 167
- 3
- 11

- 20,019
- 4
- 38
- 55
-
2I 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? – Nelson Rothermel Jan 12 '10 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 '10 at 00: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 '10 at 00: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. – Nelson Rothermel Jan 13 '10 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. – Nelson Rothermel Jan 13 '10 at 12:48
-
I was just thinking, this is more about "scripting" so maybe stackoverflow would be more appropriate. Since we use this for server administration I naturally came here. – Nelson Rothermel Jan 13 '10 at 12:54
-
The v2 ISE shows the lines when it runs a script. I find it a bit annoying in this case. That's the way the host was programmed though, and can't be "switched off". – Marco Shaw Jan 21 '10 at 23:23
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...

- 407
- 3
- 11
-
1Yeah, 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. – Nelson Rothermel Jan 21 '10 at 02:26