okay, I am now officially have began to love windows due to its great tools named powershell. Here I’ll try to run a script that will download file over the internet by using wget. Just for the record though, you can’t installed wget but you need to run the scripts in powershell (I don’t think sudo apt-get install wget will work in powershell. :-) .

firstly you have to set the execution policy in powershell.

PS C:\Users\jacobian\MyScripts> set-executionpolicy remotesigned

and to see if the policy is correctly in place do this

PS C:\Users\jacobian\MyScripts> get-executionpolicy
RemoteSigned

 
the purpose of remotesigned policy is that it can run scripts that you write but it will need confirmation when trying to run scripts downloaded over the internet.

okay now you have to open a notepad (it’s not linux and yes you have to open a notepad) and paste the following code into it.

## Get-WebFile (aka wget for PowerShell)
##############################################################################################################
## Downloads a file or page from the web
## History:
## v3.6 - Add -Passthru switch to output TEXT files
## v3.5 - Add -Quiet switch to turn off the progress reports ...
## v3.4 - Add progress report for files which don't report size
## v3.3 - Add progress report for files which report their size
## v3.2 - Use the pure Stream object because StreamWriter is based on TextWriter:
## it was messing up binary files, and making mistakes with extended characters in text
## v3.1 - Unwrap the filename when it has quotes around it
## v3 - rewritten completely using HttpWebRequest + HttpWebResponse to figure out the file name, if possible
## v2 - adds a ton of parsing to make the output pretty
## added measuring the scripts involved in the command, (uses Tokenizer)
##############################################################################################################
function Get-WebFile {
param(
$url = (Read-Host "The URL to download"),
$fileName = $null,
[switch]$Passthru,
[switch]$quiet
)

$req = [System.Net.HttpWebRequest]::Create($url);
$res = $req.GetResponse();

if($fileName -and !(Split-Path $fileName)) {
$fileName = Join-Path (Get-Location -PSProvider "FileSystem") $fileName
}
elseif((!$Passthru -and ($fileName -eq $null)) -or (($fileName -ne $null) -and (Test-Path -PathType "Container" $fileName)))
{
[string]$fileName = ([regex]'(?i)filename=(.*)$').Match( $res.Headers["Content-Disposition"] ).Groups[1].Value
$fileName = $fileName.trim("\/""'")
if(!$fileName) {
$fileName = $res.ResponseUri.Segments[-1]
$fileName = $fileName.trim("\/")
if(!$fileName) {
$fileName = Read-Host "Please provide a file name"
}
$fileName = $fileName.trim("\/")
if(!([IO.FileInfo]$fileName).Extension) {
$fileName = $fileName + "." + $res.ContentType.Split(";")[0].Split("/")[1]
}
}
$fileName = Join-Path (Get-Location -PSProvider "FileSystem") $fileName
}
if($Passthru) {
$encoding = [System.Text.Encoding]::GetEncoding( $res.CharacterSet )
[string]$output = ""
}

if($res.StatusCode -eq 200) {
[int]$goal = $res.ContentLength
$reader = $res.GetResponseStream()
if($fileName) {
$writer = new-object System.IO.FileStream $fileName, "Create"
}
[byte[]]$buffer = new-object byte[] 4096
[int]$total = [int]$count = 0
do
{
$count = $reader.Read($buffer, 0, $buffer.Length);
if($fileName) {
$writer.Write($buffer, 0, $count);
}
if($Passthru){
$output += $encoding.GetString($buffer,0,$count)
} elseif(!$quiet) {
$total += $count
if($goal -gt 0) {
Write-Progress "Downloading $url" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100)
} else {
Write-Progress "Downloading $url" "Saving $total bytes..." -id 0
}
}
} while ($count -gt 0)

$reader.Close()
if($fileName) {
$writer.Flush()
$writer.Close()
}
if($Passthru){
$output
}
}
$res.Close();
if($fileName) {
ls $fileName
}
}

save the file as wget.ps1, remember to select all file not text file when you save it.  And then it’s time for you to run the file

PS C:\Users\jacobian\MyScripts> powershell .\wget.ps1

 and you will see like the following picture.

 power

It’s a real fun learning new shell scripting language you know. :-P

source

and I want to say thanks to Joel “Jaykul” Bennett for publishing the script. :-)

4 Responses to “how to use wget in powershell”

  1. [...] how to use wget in powershell July 21, 2008 10:34 pm admin Dari Blogger – Agreegator Dari http://johnny64.wordpress.com/2008/07/21/how-to-use-wget-in-powershell/ [...]

  2. meson said

    How about giving credit to the person that actually wrote the script? Jaykul at http://www.huddledmasses.org. You can also grab the most recent version of the script at http://www.poshcode.org/417.

  3. You ought to consider giving credit when you write a post with other people’s scripts in it … and try to post the latest version.

  4. johnny64 said

    ok, thanks for reminding me though. but at least I have put your site so that everyone will knew that it was you who wrote the script.

Leave a Reply