zillabunny
zillabunny

Reputation: 335

How to do a Wget with powershell and output to a text file

I'm trying to use powershell to do a WGET and have the output go to a text file.

so far all attempts have failed.

PS C:\Program Files (x86)\GnuWin32\bin> For ($i=1; $i -lt 5; $i++) 
 {wget.exe  www.zillabunny.com | out-file C:\temp\wget.txt}

leaves an empty text file

and

For ($i=1; $i -lt 5; $i++)  {wget.exe  www.zillabunny.com} > C:\temp\wget.txt 

gives me

: The term '>' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:60 + For ($i=1; $i -lt 5; $i++) {wget.exe www.zillabunny.com} > wget.txt + ~ + CategoryInfo : ObjectNotFound: (>:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

Upvotes: 2

Views: 5730

Answers (1)

Bill_Stewart
Bill_Stewart

Reputation: 24565

One way is to enclose the for loop in a scriptblock and output all at once. For example:

& {
  for ( $i = 1; $i -le 5; $i++ ) {
    wget.exe -O - www.zillabunny.com
  }
} | out-file sample.txt

This will run the command wget.exe www.zillabunny.com 5 times and write the output to sample.txt.

If you just want to say "do something x times," you can also write it like this and dispense with the extra scriptblock:

1..5 | foreach-object {
  wget.exe -O - www.zillabunny.com
} | out-file sample.txt

Upvotes: 5

Related Questions