Echo equivalent in PowerShell for script testing

Powershell

Powershell Problem Overview


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 this?

For example, what would be the PowerShell equivalent to the following PHP code?

echo "filesizecounter: " . $filesizecounter 

Powershell Solutions


Solution 1 - Powershell

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.

Solution 2 - Powershell

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

echo "filesizecounter : $filesizecounter"

Solution 3 - Powershell

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"

Solution 4 - Powershell

Write-Host "filesizecounter : " $filesizecounter 

Solution 5 - Powershell

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.

Solution 6 - Powershell

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.

Solution 7 - Powershell

The Write-host work fine.

$Filesize = (Get-Item $filepath).length;
Write-Host "FileSize= $filesize";

Solution 8 - Powershell

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.

Solution 9 - Powershell

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

Solution 10 - Powershell

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

"filesizecounter: " + $filesizecounter

And it should output:

> filesizecounter: value

Solution 11 - Powershell

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

....

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionphillView Question on Stackoverflow
Solution 1 - PowershellRichardView Answer on Stackoverflow
Solution 2 - PowershellJustin R.View Answer on Stackoverflow
Solution 3 - PowershellJohn TView Answer on Stackoverflow
Solution 4 - PowershellaphoriaView Answer on Stackoverflow
Solution 5 - PowershellGoyuixView Answer on Stackoverflow
Solution 6 - PowershellPaul FijmaView Answer on Stackoverflow
Solution 7 - Powershellali DarabiView Answer on Stackoverflow
Solution 8 - PowershelllauxjpnView Answer on Stackoverflow
Solution 9 - PowershellRuss Van BertView Answer on Stackoverflow
Solution 10 - PowershellPeterView Answer on Stackoverflow
Solution 11 - PowershellLuisView Answer on Stackoverflow