PowerShell script, showing commands run
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!
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:
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...
Solution 4:
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.