Download file with PowerShell

Downloads and saves a file in the current working directory of PowerShell.

Can you use the previous working directory ($pwd) or change it to a fixed location: “C:\Download” or by first change the directory in PowerShell.
Examples of changing the directory:

$storageDir = $pwd

# or:

$storageDir = "C:\Downloads"

#or:

cd C:\Users\Teusje\Documents
$storageDir = $pwd

 

Below is the script to download a file via PowerShell. You can run it directly in PowerShell:

$storageDir = $pwd
$webclient = New-Object System.Net.WebClient
$url = "http://teusje.files.wordpress.com/2011/02/giraffe-header1.png"
$file = "$storageDir\myNewFilename.jpg"
$webclient.DownloadFile($url,$file)

Have fun! ;)

About these ads

9 thoughts on “Download file with PowerShell

  1. Pingback: Downloading files with PowerShell using the BitsTransfer module | Teusje

  2. article was just what I needed. However, pwd often means ‘present working directory’ in the *nix world, so that may be something to keep in mind, if only for the sake of posterity.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s