• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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?
0
ndalmolin_13
Asked:
ndalmolin_13
1 Solution
 
x-menIT super heroCommented:
do {$x++;"$x" >> test.txt } while ($X -le 10)
0
 
ndalmolin_13Author Commented:
That did the trick.  Thanks for the quick reply.
0
 
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
$x = 0
do
{
++$x;
"The count is $x right now" | Add-Content  c:\temp\rahman2.txt
} while ($x -le 100)
0
 
pony10usCommented:
You can't use write-host as it doesn't return any objects. It simply sends them to the host.
0

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now