In this video tutorial, Brien Posey demonstrates three methods for downloading files using PowerShell. Posey explains the use of variables to store the URL and file path, which simplifies the download commands.
The following transcript has been lightly edited for clarity and length.
Brien Posey: Hello, greetings, and welcome. I'm Brien Posey. In this video, I want to show you three different methods for downloading files using PowerShell.
So, here we are in PowerShell, and, as you can see, I've already entered two different commands. These two commands create variables. The first one is called $URL, and it simply points to a URL associated with a sample file that we're going to be downloading.
In case you're curious about this file, it is just a sample file. I'll switch over to my browser, and you can see what the sample file looks like. It's just a PDF file. And if you look at the URL right here in my browser, you can see that it's the same URL that I associated with the $URL variable in PowerShell.
The next variable is called $Path. And $Path is equal to C:\Users\Brien\Sample.pdf. In other words, the $Path variable contains the path and the file name that I want to use for the file that I download. So, when I download the sample PDF file, I’m going to be storing it in the C:\Users\Brien folder using the file name Sample. pdf.
The reason why I went ahead and created these variables is so that I can reuse them since I'm going to be showing you three different download methods. But the other reason is that creating the variables ahead of time just makes the download commands a little bit simpler to type because you don't have to worry about typing the full URL or the full path within the command.
So, with that said, let's go ahead and take a look at these commands that I want to show you for downloading files.
Before I do that, I'm just going to type Get-ChildItem. When I do, you can see that right now there are no files in this folder. We have a few subfolders, but there aren't any files that exist. So, we are going to be downloading the sample file.
PowerShell Download Method #1: Invoke-WebRequest
The first method that I want to show you is the method that I tend to use most often when I’m downloading files from PowerShell. That’s to use the Invoke-WebRequest command.
To use this command, we’re going to type Invoke-WebRequest.
Next, we have to specify the -URI parameter. And that’s URI, not URL. If you type -URL, you’ll get an error when you run this command.
Next, we have to provide the URL for the file that we’re going to be downloading. And remember, we already mapped the URL to a variable called $URL. So, instead of typing the full URL, I’m just going to reference the variable.
Then the next thing that we have to provide is the -OutFile switch.
And so finally, we need to provide the path and file name for the file that we want to download. So, rather than type the full path, I’m just going to reference the $Path variable that I created earlier.
The full command:
Invoke-WebRequest -URI $URL -OutFile $Path
I’ll press Enter. And that quickly the file was downloaded.
So, let’s take a look and confirm that the file has been downloaded. If you look at the very bottom of the directory, you can see the Sample.pdf that we just downloaded. So, the file has indeed been downloaded.
I’m going to go ahead and erase this file:
And let’s make sure it’s gone. The file has been erased.
Let’s move on to the next download method.
PowerShell Download Method #2: New-Object
The next download method involves creating an object. The object that we're going to create is an object of type System.Net.WebClient. And then once we've created that object, we're going to call a method of the object called download file.
So, let's take a look at how this works.
One thing that I mentioned before I typed this is that this particular command is extremely picky about where you insert spaces. So, to make this command work, you're going to have to type it exactly the way that I've typed it.
So, I'm going to begin by typing parenthesis and then New-Object.
And incidentally, as I type this, you'll notice that just after my name, Brien, the greater-than sign (>) is red. Anytime that you see a red greater-than sign, it means that there's a syntax error and that if you press enter, you're going to receive an error. For example, if I were to press enter right now, the command is incomplete, and I'm taken to this. I'll go ahead and break out of this.
At any rate, I'll type:
I’ll press Enter.
And let's check to make sure that the file was downloaded. And you can see that the file has been downloaded.
So, let me go ahead and remove this file, and we'll verify that it's gone. And the file has been removed.
PowerShell Download Method #3: BITS Transfer
The last method that I want to show you is to perform a BITS transfer.
So, BITS is a service that is built into Windows. "BITS” stands for Background Intelligent Transfer Services.
To use BITS, you have to make sure that the underlying service has been started. So, what you would do is go to the Windows Run prompt and type services.msc.
I’ve already got the Service Control Manager open, but let’s go ahead and close that out and instead just do this from the beginning. So, I'm going to right-click on the Start button. And I'll go to Run, and I'll type services.msc. And when I press Enter, that's going to open the Service Control Manager, which you can see right here, I'll make this a little bigger. And we need to locate the BITS service. So here you can see the Background Intelligent Transfer Service – that's BITS. And you can see that it has a Startup Type of Manual, and it's not currently running. So, we need to start the service. The way that you would do that is to right-click on the service and then just click Start. And that's going to launch the service. And now you can see that the Status has changed to Running.
So, now that the service is running, I'm going to go ahead and close out the Service Control Manager. And let's do a BITS transfer.
The way that we do this is by using the Start-BitsTransfer cmdlet. And then I’ll specify the -Source parameter, followed by the $URL. And once again, I'll use the variable that I set up. Next, we have to specify the -Destination parameter, followed by the $Path, and again I'll use my variable.
The full command:
Start-BitsTransfer -Source $URL -Destination $Path
I'll press Enter.
You'll notice that that method is just a little bit slower than the other methods, but that's okay.
Let's verify that the file was downloaded. And you can see that the download has been completed.
So, those are three methods of downloading files using PowerShell.
About the authorBrien Posey is a bestselling technology author, speaker, and 21x Microsoft MVP. In addition to his ongoing work in IT, Posey has trained as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space..