PowerShell script, showing commands run

57,186

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Share:
57,186

Related videos on Youtube

Nelson Rothermel
Author by

Nelson Rothermel

Updated on September 17, 2022

Comments

  • Nelson Rothermel
    Nelson Rothermel almost 2 years

    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!

    • Admin
      Admin almost 2 years
      None of the answers get us back to "echo on" equivalent. Any update in the past 12 years?
  • Nelson Rothermel
    Nelson Rothermel over 14 years
    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?
  • Helvick
    Helvick over 14 years
    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.
  • Nelson Rothermel
    Nelson Rothermel over 14 years
    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
    Nelson Rothermel over 14 years
    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
    Nelson Rothermel over 14 years
    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
    Nelson Rothermel over 14 years
    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.
  • Marco Shaw
    Marco Shaw over 14 years
    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".
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 5 years
    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.