PS: Write-host will not output to out-file

27 Aug

I have a quick powershell script to list print drivers in remote servers (which I copied with the help of Mr. google)


$colItems = get-wmiobject -class "Win32_PrinterDriver" -namespace "root\CIMV2" `
-computername $strComputer
write-Host "Computer: " $strComputer
foreach ($objItem in $colItems) {
write-Host "Name: " $objItem.Name

From powershell console, I type the following hoping to capture the output into a file

get-content .\hosts.txt | foreach {.\myscript $_} | out-file .\myfile.txt -append

However, I can only see the output from my console but nothing in myfile.txt.

If  you want to capture output to file, you need to replace write-host with write-output. As write-host does what it says, writes to host console and nothing more! So after change the script with write-output, the output file got generated.

Leave a comment

Posted by on August 27, 2010 in powershell, Windows



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: