Echo equivalent in PowerShell for script testing

前端 未结 11 1551
梦如初夏
梦如初夏 2020-12-07 10:08

I would like to output variables and values out in a PowerShell script by setting up flags and seeing the data matriculate throughout the script.

How would I do thi

相关标签:
11条回答
  • 2020-12-07 10:26

    echo is alias to Write-Output although it looks the same as Write-Host.

    It isn't What is the difference between echo and Write-Host in PowerShell?.

    echo is an alias for Write-Output, which writes to the Success output stream. This allows output to be processed through pipelines or redirected into files. Write-Host writes directly to the console, so the output can't be redirected/processed any further.

    0 讨论(0)
  • 2020-12-07 10:31

    By far the easiest way to echo in powershell, is just create the string object and let the pipeline output it:

    $filesizecounter = 8096
    "filesizecounter : $filesizecounter"
    

    Of course, you do give up some flexibility when not using the Write-* methods.

    0 讨论(0)
  • 2020-12-07 10:31

    Try Get-Content .\yourScript.PS1 and you will see the content of your script.

    also you can insert this line in your scrip code:

    get-content .\scriptname.PS1
    script code
    script code
    

    ....

    0 讨论(0)
  • 2020-12-07 10:34

    There are several ways:

    Write-Host: Write directly to the console, not included in function/cmdlet output. Allows foreground and background colour to be set.

    Write-Debug: Write directly to the console, if $DebugPreference set to Continue or Stop.

    Write-Verbose: Write directly to the console, if $VerbosePreference set to Continue or Stop.

    The latter is intended for extra optional information, Write-Debug for debugging (so would seem to fit in this case).

    Additional: In PSH2 (at least) scripts using cmdlet binding will automatically get the -Verbose and -Debug switch parameters, locally enabling Write-Verbose and Write-Debug (i.e. overriding the preference variables) as compiled cmdlets and providers do.

    0 讨论(0)
  • 2020-12-07 10:36

    Powershell has an alias mapping echo to Write-Output, so:

    echo "filesizecounter : $filesizecounter"

    0 讨论(0)
提交回复
热议问题