Link to home
Start Free TrialLog in
Avatar of ndalmolin_13
ndalmolin_13Flag for United States of America

asked on

Help redirecting powershell output to a text file

Hello Experts,

For some reason this one is stumping me.  I have the following code that displays a message to the screen and I want it to write to a text file.

$x = 0
do
{
Write-Host "The count is "($x++)" right now"
} while ($x -le 100)

I've tried the following:

$x = 0
do
{
Write-Host "The count is "($x++)" right now" >> c:\text.txt
} while ($x -le 100)

and

$x = 0
do
{
Out-file "The count is "($x++)" right now" | c:\text.txt
} while ($x -le 100)

and

$x = 0
do
{
Write-Host "The count is "($x++)" right now" | out-file c:\test.txt
} while ($x -le 100)

What am I missing?
ASKER CERTIFIED SOLUTION
Avatar of x-men
x-men
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ndalmolin_13

ASKER

That did the trick.  Thanks for the quick reply.
$x = 0
do
{
++$x;
"The count is $x right now" | Add-Content  c:\temp\rahman2.txt
} while ($x -le 100)
You can't use write-host as it doesn't return any objects. It simply sends them to the host.