How to output variables for testing in PowerShell with echo?

How can I use echo in PowerShell to output variables and their values for script testing? I want to set up flags and observe how the data flows through the script. For example, in PHP, I would use the following code to display a variable’s value:

echo "filesizecounter: " . $filesizecounter;

What is the PowerShell equivalent to achieve this?

Here are several ways to output variables and values in PowerShell:

Write-Host: Outputs directly to the console, but it doesn’t include the output in the function or cmdlet’s return value. It allows you to set foreground and background colors.

Write-Debug: Outputs to the console if $DebugPreference is set to Continue or Stop. This is useful for debugging purposes.

Write-Verbose: Outputs to the console if $VerbosePreference is set to Continue or Stop. It’s intended for providing additional, optional information.

In PowerShell, Write-Debug is particularly suited for debugging, making it a good choice for this case.

Additional Note: In PowerShell 2.0 and later, scripts using CmdletBinding automatically receive -Verbose and -Debug switch parameters. These parameters locally enable Write-Verbose and Write-Debug, overriding the preference variables, similar to how compiled cmdlets and providers behave.

PowerShell has an alias that maps echo to Write-Output, so you can use the following command:

echo "filesizecounter: $filesizecounter"

PowerShell does support string interpolation. In PHP, the following two lines are equivalent:

echo "filesizecounter: " . $filesizecounter;
echo "filesizecounter: $filesizecounter";

Similarly, in PowerShell, you can achieve the same result with: Write-Host “filesizecounter: $filesizecounter”