How to download files in PowerShell. In this part of the PowerShell training series, we will teach you how to download a file in PowerShell. PowerShell command line environment allows you to do all the things that a user can do with the Windows graphical environment.
Before the creation and development of PowerShell, Windows users did not have the ability to download files via the CMD command line. Therefore, to download a file by commands, they had to use different scripts. But PowerShell, with its power and modular capabilities, allows you to easily download a file.
The command we teach in this article is very similar to the wget command in Linux.
Download file with PowerShell
Download the file with the Invoke-WebRequest command
The Invoke-WebRequest command is a very complete command for downloading all types of files with a direct link.
This command, like other PowerShell commands, has Aliases that make it easy to use.
You can use the wget, curl and iwr commands instead of the Invoke-WebRequest command.
Now, with the explanations that we have provided to you, we will explain and use this command.
Invoke-WebRequest command structure
For example, to download a file from a site, the following command will be entered:
Invoke-WebRequest -Uri “https://eldernode.com/blog/file.txt”
This command has many parameters that will help you to download the file with different features.
In this article, we will explain some very important parameters of this command.
Invoke-WebRequest command parameters
Outfile: This parameter can save the name of the file being downloaded to another path and name.
Credential: This parameter allows us to send a username and password to allow access to download the file.
DisableKeepAlive: To enable or disable KeepAlive when downloading.
MaximumRedirection: The number of times a link is redirected and the download command still stands and follows the next link. By default it will reject 5 redirects and if it continues, the download will stop.
Method: This parameter is used to select the types of request submission methods. Supported methods include:
Default, Delete, Get, Head, Merge, Option, Patch, Post, Put and Trace.
Proxy: Use a proxy to connect to the Internet and download files.
The mentioned parameters are just some of the most important ones. There are more complete and advanced parameters that you can get by marking ? or using the Get-Help command.
Now, after explaining and learning how to use the command, pay attention to the following examples for better learning.
2 examples for the Invoke-WebRequest command
Invoke-WebRequest -Uri "https://www.eldernode.com/file1.txt" -OutFile "D:\"
By the above command, file1.txt is downloaded from Eldernode.com and stored on drive D.
Invoke-WebRequest -Uri "https://www.eldernode.com/file1.txt" -OutFile " D:\" -Credential "administrator"
In the above example , PowerShell downloads file1.txt, and when it asks for a username from the site, PowerShell sends the Administrator username as a username.
Likewise, you can use the Invoke-WebRequest command to download the file in PowerShell.
This command has other uses that, like other PowerShell commands, provide different output after merging with other commands.
For example, entering the following command will list all the links within a web page for you:
(Invoke-WebRequest -Uri "https://eldernode.com").Links.Href
Dear user, we hope you would enjoy this tutorial, you can ask questions about this training in the comments section, or to solve other problems in the field of Eldernode training, refer to the Ask page section and raise your problem in it as soon as possible. Make time for other users and experts to answer your questions.