337

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 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
phill
  • 13,434
  • 38
  • 105
  • 141
  • Check out the script-editor/debugger that comes with [PowerGUI](http://www.powergui.org/index.jspa). It may be suitable for what you are doing. I understand that Powershell 2 comes with a debugger too (but I haven't tried it). – dan-gph Apr 02 '09 at 12:54

11 Answers11

374

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.

Jacktose
  • 709
  • 7
  • 21
Richard
  • 106,783
  • 21
  • 203
  • 265
  • 6
    The nice thing about Write-Debug and Write-Verbose is that you can leave your debug statements in your code, and just turn on the output when you need it using the appropriate preference variable. Saves time and is a nice feature for other users of your functions. – JasonMArcher Apr 02 '09 at 20:04
  • 6
    You also want to get familiar with Out-String, because with any non-trivial object, you'll need to use that to convert the object to a display-able string before using any of those three Write-* cmdlets. – Jaykul Nov 02 '09 at 07:11
166

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

echo "filesizecounter : $filesizecounter"
galoget
  • 722
  • 9
  • 15
Justin R.
  • 23,435
  • 23
  • 108
  • 157
  • 10
    `echo` is an alias to `Write-Output` as per Microsoft's [TechNet](https://technet.microsoft.com/en-us/library/hh849921.aspx) and discussed [here](http://stackoverflow.com/a/17623902/857209) – Glenn Lawrence Jul 02 '15 at 04:30
  • 4
    Thanks Glenn, it looks like the alias has been remapped in the 6 years since I posted this. Which is good, one really shouldn't use Write-Host as it bypasses the pipeline. I've updated my reply to indicate this. Thanks! – Justin R. Dec 07 '15 at 21:38
  • 6
    Why wouldn't you want to bypass the pipeline if all you want to do is echo "debug" output to the console to trace execution of a script? Write-Output has a weird side-effect where the content of the last call to Write-Output will be left on the stack, overriding any variable you attempt to explicitly return from a function. Side-effects like that feel like an awfully screwy way for a language to behave. – Craig Tullis Aug 02 '17 at 02:07
41

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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John T
  • 23,735
  • 11
  • 56
  • 82
31
Write-Host "filesizecounter : " $filesizecounter 
aphoria
  • 19,796
  • 7
  • 64
  • 73
14

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.

Goyuix
  • 23,614
  • 14
  • 84
  • 128
12

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.

Community
  • 1
  • 1
Paul Fijma
  • 467
  • 4
  • 9
10

The Write-host work fine.

$Filesize = (Get-Item $filepath).length;
Write-Host "FileSize= $filesize";
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
ali Darabi
  • 337
  • 3
  • 3
8

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.

lauxjpn
  • 4,749
  • 1
  • 20
  • 40
7

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://learn.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

Russ Van Bert
  • 752
  • 11
  • 22
5

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

"filesizecounter: " + $filesizecounter

And it should output:

filesizecounter: value

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter
  • 37,042
  • 39
  • 142
  • 198
2

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

....

Chillie
  • 1,030
  • 10
  • 28
Luis
  • 29
  • 1
  • 1
    Doesn't show actual values processed (e.g. env var substitution) so not an answer to the question asked. – iisystems Aug 26 '19 at 21:23