Windows – How to output something in PowerShell


I am running a PowerShell script from within a batch file. The script fetches a web page and checks whether the page's content is the string "OK".

The PowerShell script returns an error level to the batch script.

The batch script is executed by ScriptFTP, an FTP automation program. If an error occurs, I can have ScriptFTP send the full console output to the administrator via E-Mail.

In the PowerShell script, I would like to output the return value from the web site if it is not "OK", so the error message gets included in the console output, and thus in the status mail.

I am new to PowerShell and not sure which output function to use for this. I can see three:

  • Write-Host
  • Write-Output
  • Write-Error

What would be the right thing to use to write to the Windows equivalent of stdout?

Best Solution

Simply outputting something is PowerShell is a thing of beauty - and one its greatest strengths. For example, the common Hello, World! application is reduced to a single line:

"Hello, World!"

It creates a string object, assigns the aforementioned value, and being the last item on the command pipeline it calls the .toString() method and outputs the result to STDOUT (by default). A thing of beauty.

The other Write-* commands are specific to outputting the text to their associated streams, and have their place as such.