“echo on” in powershell or how do I make Powershell output the command lines of all the commands, INCLUDING the native ones invoked by the script?

Question:

My question may seem duplicate of PowerShell “echo on”, but it is not.

I am not interested in capturing the command output, but in the command line itself of every command executed by the script, including the native commands.

This is what “echo on” in cmd does and this is what I am looking for. Set-PSDebug -Trace 1 does not do it and neither passing the -Verbose flag.

So far I have not see a way except outputing them myself, which is a huge pain in itself.

So, can Powershell do what “echo on” does in cmd?

EDIT 1

Not ideal, but I would accept an answer suggesting to use a wrapper function which would receive a command (native or powershell) with parameters and run the command while faithfully logging the respective command line. Of course, the wrapper function code should be part of the answer.

EDIT 2

The following trivial example demonstrates why Set-PSDebug -Trace 1 does not do it:

Please, observe:

EDIT 3

For comparison, observe an equivalent script in cmd with echo on:

EDIT 4

start-transcript does not do it either:

As you can see it does not contain the command line.

Answer:

If Event logs is an option, start tracing by enabling this Group Policy.
Administrative Templates -> Windows Components -> Windows PowerShell

See Microsoft Docs – Script Tracing and Logging

Then you would of course need to parse the Event logs accordingly…

Source:

“echo on” in powershell or how do I make Powershell output the command lines of all the commands, INCLUDING the native ones invoked by the script? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply