3 Ways to Download a File in PowerShell

PowerShell users can use the Invoke-Webrequest, New-Object, or Start-BitsTransfer cmdlets to download files. Here's how each method works.

Brien Posey

June 14, 2022

3 Min Read
keyboard with a green button that says "download"
Alamy

If you use PowerShell to manage Windows, sooner or later you will need to download a file. This is especially true for Windows Server, since best practices have long warned against browsing the internet from a web browser installed on a server.

PowerShell is surprisingly flexible when it comes to file downloads. In fact, there are at least three different ways that you can download a file in PowerShell.

  1. Invoke-WebRequest

  2. New-Object

  3. Start-BitsTransfer

Before explaining how, I want to note that each of the three download techniques accepts a common set of parameters. These parameters include A), the URL that points to the file that is being downloaded, and B), a path to where the file will be stored on your local system once it is downloaded.

As a best practice, I recommend storing the path and the URL in variables. For the purposes of this article, I will be using the variables $URL and $Path. Here is an example of how you would declare these variables, although you will want to replace the values with your own.

$URL = “http://192.168.0.1/MyFile.txt”$Path=”C:FilesMyFile.txt”

Notice that the path not only contains the file system location but also the filename to use for the file you will download. This filename does not have to match the filename within the URL. In fact, you can use a different filename as a way of renaming a file when you download it.

Related:Use PowerShell to Initialize a Disk and Create Partitions

1. Invoke-WebRequest Cmdlet

The Invoke-WebRequest cmdlet tends to be my go-to cmdlet for downloading files. Any of the three cmdlets that I show you will work, however.

The syntax used by the Invoke-WebRequest cmdlet is simple. Here is what a download command might look like:

Invoke-WebRequest -URI $URL -OutFile $Path

Note that the URL is mapped to a parameter called -URI, not -URL. If you try to name the parameter -URL, you will receive an error message like the one shown in Figure 1.

PowerShell screenshot shows that the parameter -URL will produce an error message

PowerShell Download 1

Figure 1. Be sure to enter -URI as the parameter name, not -URL.

In Figure 2, you can see what the download process looks like, along with the downloaded file.

Screenshot shows Invoke-WebRequest cmdlet used to download a file

PowerShell Download 2

Figure 2. This is how you use the Invoke-WebRequest cmdlet to download a file.

2. New-Object Cmdlet

You can also use the New-Object cmdlet to download a file in PowerShell.

The New-Object cmdlet, as you may already know, can be used to create a .NET framework object or a COM object. If your goal is to download a file, you will need to create a System.Net.WebClient object. When you create this object, you must call the DownloadFile method and provide values for the URL and for the output path.

Here is an example of what this command looks like:

(New-Object System.Net.WebClient).DownloadFile ($URL, $Path)

When using this command, make sure the New-Object cmdlet is in parentheses. Figure 3 shows you the command in action.

Screenshot shows New-Object cmdlet used to download a file in PowerShell

PowerShell Download 3

Figure 3. This is how to use the New-Object cmdlet to download a file.

3. Start-BitsTransfer

The third way that you can download files in PowerShell is to perform a BITS transfer.

BITS refers to the Background Intelligent Transfer Service. This service is a part of the Windows operating system, and you will need to have BITS enabled for this technique to work.

The Start-BitsTransfer command is just as easy to use as the other two commands. The command looks like this:

Start-BitsTransfer -Source $URL -Destination $Path

Figure 4 offers an example of the command. Although the figure does not show it, BITS transfers include an onscreen progress bar that you can use to monitor the download.

Screenshot shows BITS transfer used to download a file in PowerShell

PowerShell Download 4

Figure 4. This is how you perform a BITS transfer using PowerShell.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like