Echo equivalent in PowerShell for script testing

前端 未结 11 1550
梦如初夏
梦如初夏 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:13

    The Write-host work fine.

    $Filesize = (Get-Item $filepath).length;
    Write-Host "FileSize= $filesize";
    
    0 讨论(0)
  • 2020-12-07 10:18

    I don't know if it's wise to do so, but you can just write

    "filesizecounter: " + $filesizecounter
    

    And it should output:

    filesizecounter: value

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

    PowerShell has aliases for several common commands like echo. Type the following in PowerShell:

    Get-Alias echo
    

    to get a response:

    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Alias           echo -> Write-Output
    

    Even Get-Alias has an alias gal -> Get-Alias. You could write gal echo to get the alias for echo.

    gal echo
    

    Other aliases are listed here: https://docs.microsoft.com/en-us/powershell/scripting/learn/using-familiar-command-names?view=powershell-6

    cat dir mount rm cd echo move rmdir chdir erase popd sleep clear h ps sort cls history pushd tee copy kill pwd type del lp r write diff ls ren

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

    It should also be mentioned, that Set-PSDebug is similar to the old-school echo on batch command:

    Set-PSDebug -Trace 1
    

    This command will result in showing every line of the executing script:

    When the Trace parameter has a value of 1, each line of script is traced as it runs. When the parameter has a value of 2, variable assignments, function calls, and script calls are also traced. If the Step parameter is specified, you're prompted before each line of the script runs.

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

    PowerShell interpolates, does it not?

    In PHP

    echo "filesizecounter: " . $filesizecounter 
    

    can also be written as:

    echo "filesizecounter: $filesizecounter" 
    

    In PowerShell something like this should suit your needs:

    Write-Host "filesizecounter: $filesizecounter"
    
    0 讨论(0)
  • 2020-12-07 10:26
    Write-Host "filesizecounter : " $filesizecounter 
    
    0 讨论(0)
提交回复
热议问题