In this post, we will demonstrate the possible ways to download a file using PowerShell.
How to download a file using PowerShell?
PowerShell is a scripting shell to automate the management of the system, but it can also be used to download a file.
The methods are as follows for downloading a file from the source link with PowerShell.
- Invoke-WebRequest: The primary cmdlet
- iwr: is an alias of Invoke-WebRequest cmdlet
- wget: is an alias of Invoke-WebRequest cmdlet
- curl: is also an alias of Invoke-WebRequest cmdlet
Example 1: Using Invoke-WebRequest to download a file
In this example, the Invoke-webRequest is used to download the file from the source location to the absolute path of the local drive.
-OutFile "C:\Users\powershell\Documents\PowerShell\webrequest_powershell.html"
ls "C:\Users\powershell\Documents\PowerShell"
The above code is described as,
– the path of the file to be downloaded is passed to the “-Uri” parameter
– the destination path is defined using the “-OutFile” parameter
As you can see in the above screenshot, the file is downloaded to the destination path, without any error.
Example 2: Using iwr to download a file
The “iwr” is an alias of the Invoke-WebRequest cmdlet that can also be used to download files from the link. The example code is used to download a file using “iwr”:
-OutFile "C:\Users\powershell\Documents\PowerShell\iwr_powershell.html"
ls "C:\Users\powershell\Documents\PowerShell"
As you can see in the above screenshot, the file is downloaded successfully with the help of “iwr”.
Example 3: Using wget to download a file
Like “iwr”, the wget is also an alias of the Invoke-WebRequest cmdlet. With the same syntax just replace the Invoke-WebRequest with “wget” to download the file. The example is as follows.
-OutFile "C:\Users\powershell\Documents\PowerShell\wget_powershell.html"
ls "C:\Users\powershell\Documents\PowerShell"
The downloaded file can be seen in the download path as per the above results.
Example 4: Using curl to download a file
The file from the link can be downloaded by using the Invoke-WebRequest alias command, curl. The below given an example will download the file from the source to the local drive path.
-OutFile "C:\Users\powershell\Documents\PowerShell\curl_powershell.html"
ls "C:\Users\powershell\Documents\PowerShell"
It can be noticed in the attached screenshot that the PowerShell.html is downloaded to the path successfully.
You have learned the four different methods of downloading files from the source link to the local drive path.
Conclusion
A file can be downloaded with Microsoft PowerShell via the Invoke-WebRequest command. The PowerShell offers three aliases of Invoke-WebRequest i.e., iwr, curl, and wget. This post demonstrates various methods to download a file using PowerShell. Although the working of all these four cmdlets is the same, therefore, you can use any of these to download a file using PowerShell.