How to easily download a file with PowerShell

Reading time icon 3 min. read


Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help Windows Report sustain the editorial team. Read more

PowerShell is a handy, free task automation tool provided by Microsoft that lets you manage and, as you might’ve guessed from its name, automate your tasks on Windows with ease. However, that’s not all, though.

You can, for instance, also download internet files thanks to your PowerShell app. In this piece, we dive into the most straightforward method to get files with PowerShell. Hereโ€™s how you can get started.

How to download a file with PowerShell

One of the most common ways to download your file with PowerShell is by using the URL of the file you’d like to download. That’s precisely what we’ll be covering in this short guide. To complete your download, youโ€™ll be using the Invoke-WebRequest cmdlet.

Also, before we head to the actual download, we’d also want to specify that youโ€™ll need a specific destination where you can store the downloaded file. Youโ€™ll have to add a particular parameter to your cmdlet file.

Hereโ€™s what the complete syntax will look like:

Invoke-WebRequest -Uri <source> -OutFile <destination>

Depending on where youโ€™d like to download the files from and the place youโ€™d like to put your files, replace the source and destination variable with the URL of the file youโ€™d like to download and the place youโ€™d like to download it to.

After you’ve launched the PowerShell, type in the following dummy command. This is what it will look like:

# Source file location
$source = 'https://www.majorgeeks.com/files/details/4diskclean_free.html/4dskcln.exe'
# Destination to save the file
$destination = 'C:\programfiles\newfolder'
#Download the file
Invoke-WebRequest -Uri $source -OutFile $destination

Here, the command downloads the file source from ‘https://www.majorgeeks.com/files/details/4diskclean_free.html/4dskcln.exe’, and then saves it to โ€˜c:\programfiles\newfolderโ€™.

This should download the required files fairly simply. However, in some cases, you might get asked for login access before you can proceed with the download. You can do that by adding the -Credential parameter. Just add the following command to your code:

$credential = Get-Credential

Hereโ€™s what the complete command will look like now:

$credential = Get-Credential
$source = 'https://www.majorgeeks.com/files/details/4diskclean_free.html/4dskcln.exe'
$destination = 'C:\programfiles\newfolder'
Invoke-WebRequest -Uri $source -OutFile $destination -Credential $credential

Thatโ€™s it. Your download will begin.

Downloading a file with PowerShell without any hassles

Downloading files with PowerShell doesnโ€™t have to be super complicated. With the method we’ve explained above, we believe it was a breeze for you. However, donโ€™t hit the brakes just now. PowerShell packs tons of commands that can help you do just about anything on your PC. So, donโ€™t stop just now; keep exploring things out there.

User forum

0 messages