Download a file from the Internet using PowerShell

Download a file from the Internet using PowerShell

Q. How can I download a file using PowerShell from the Internet?

A. Using the Invoke-WebRequest it's possible to download content from a web location and save to a local file. Below is an example downloading a file from my site however you could change to download anything:

$folder = "d:\temp"
$url= "http://www.savilltech.com/images/MasterClassLogoMedium.png"
$req = [System.Net.HttpWebRequest]::Create($url)
$req.Method = "HEAD"
$response = $req.GetResponse()
$fUri = $response.ResponseUri
$filename = [System.IO.Path]::GetFileName($fUri.LocalPath); 
$response.Close()
$target = join-path $folder $filename 
Invoke-WebRequest -Uri $url -OutFile $target 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish