Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell "echo on"

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.

like image 787
Nelson Rothermel Avatar asked Jan 14 '10 12:01

Nelson Rothermel


People also ask

How do I run echo in PowerShell?

Type “Write-Output” in the scripting pane of the “PowerShell ISE“, and then write hyphen (-). A dropdown menu will be activated, which contains the supported parameter: For instance, the echo/Write-Output command prints the output as an individual expression.

What does $_ in PowerShell mean?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

What is ${} in PowerShell?

The ${} notation actually has two uses; the second one is a hidden gem of PowerShell: That is, you can use this bracket notation to do file I/O operations if you provide a drive-qualified path, as defined in the MSDN page Provider Paths.

How do I use verbose in PowerShell?

Typically, the verbose message stream is used to deliver more in depth information about command processing. By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreference variable or using the Verbose common parameter in any command.


2 Answers

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://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-psdebug?view=powershell-6

like image 134
wisbucky Avatar answered Sep 27 '22 20:09

wisbucky


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.

like image 45
Keith Hill Avatar answered Sep 27 '22 22:09

Keith Hill