Thursday, May 14, 2015

wget equivalent in PowerShell

As some have pointed out there is not the equivalent for wget in PowerShell 1 or 2. It is included as Invoke-WebRequest in PowerShell 3.

To implement it yourself in PowerShell 1 or 2, you can use the following:



## Declare a function that takes source and destination arguments
Function Get-Webclient ($url, $out) {
$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
$request = New-Object System.Net.WebCLient
$request.UseDefaultCredentials = $true ## Proxy credentials only
$request.Proxy.Credentials = $request.Credentials
$request.DownloadFile($url, $out)
}

## Call the function with our source and destination.
Get-Webclient "http://www.google.com" "C:\Foo3.txt"


Credit to this post.

No comments: