tags:

views:

2972

answers:

6

This is probably a very obvious question.. 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 this PHP:

echo "filesizecounter : " . $filesizecounter 

?

+3  A: 

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"
John T
+9  A: 

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.

Richard
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
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
+1  A: 
Write-Host "filesizecounter : " $filesizecounter
aphoria
A: 

Check out the script-editor/debugger that comes with PowerGUI. 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).

dangph
+2  A: 

Powershell has an alias mapping echo to Write-Host, so:
echo "filesizecounter : $filesizecounter" (PHP)
echo "filesizecounter : $filesizecounter" (Powershell)

fatcat1111
A: 

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