How to easily download a file with PowerShell - OnMSFT.com
We're hedging our bets! You can now follow OnMSFT.com on Mastadon or Tumblr!

How to easily download a file with PowerShell

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.

Share This Post:

Older

Here's what's announced as coming to Microsoft 365 this week

Soccer Celebration comes to Minecraft

Newer