How to easily download a file with PowerShell
3 min. read
Published on
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