11 years, 1 month ago

The equivalent of wget in PowerShell

Lately I have been using PowerShell on Windows to script and automate a number of system administration tasks. PowerShell is Microsoft’s next generation replacement for good old CMD.EXE and it borrows a lot of powerful concepts from shells and scripting environments such as Ksh, Perl, and Tcl. The probably biggest difference between PowerShell and the Unix shell environment is that PowerShell is object-oriented as opposed to the text/stream orientation of the classic Unix shell. Either way it is an incredibly powerful tool although it takes time to get used to the object-orientation and new commands.

On the Unix command line I often use ‘wget’ when I need to fetch something from the Internet – be it a web page, an executable, security patch etc. Now, instead of having to open up your browser in Windows or boot up Cygwin you can simply use PowerShell to do the same.

$client = new-object System.Net.WebClient
$client.DownloadFile("https://download.mozilla.org/?product=firefox-19.0.2&os=win&lang=en-US", "firefox.exe")

This will create a new System.Net.WebClient .NET object and download the file immediately.  Very neat.

Note: if you are already using PowerShell v3, you can simpy use the cmd-let Invoke-WebRequest, which does exactly the same thing.