If you use PowerShell to manage Windows, sooner or later you will need to download a file. This is especially true for Windows Server, as 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 uploads. In fact, there are at least three different ways to upload a file in PowerShell.
- New object
Before explaining how, I want to point out that each of the three download techniques accepts a common set of settings. These parameters include A), the URL that points to the file being downloaded, and B), a path to where the file will be stored on your local system once downloaded.
As a best practice, I recommend storing the path and URL in variables. For the purposes of this article, I will use the $URL and $Path variables. Here is an example of how you would declare these variables, although you want to replace the values with your own.
$URL = “http://192.168.0.1/MyFile.txt” $Path=”C:FilesMyFile.txt”
Note that the path not only contains the file system location, but also the filename to use for the file you are about to upload. This filename does not have to match the filename in the URL. In fact, you can use a different filename to rename a file when you download it.
1. Invoke-WebRequest cmdlet
The Invoke-WebRequest cmdlet tends to be my go-to cmdlet for uploading files. However, any of the three cmdlets I show you will work.
The syntax used by the Invoke-WebRequest cmdlet is simple. This 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 -URL parameter, you will receive an error message like the one shown in Figure 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.
Figure 2. Here’s how to use the Invoke-WebRequest cmdlet to upload a file.
2. New Object cmdlet
You can also use the New-Object cmdlet to upload 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 upload 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, ensure that the New-Object cmdlet is in parentheses. Figure 3 shows you the command in action.
Picture 3. Here’s how to use the New-Object cmdlet to upload a file.
The third way to upload files in PowerShell is to perform a BITS transfer.
BITS refers to Background Intelligent Transfer Service. This service is part of the Windows operating system and you must enable BITS 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 provides an example of the command. Although the figure does not show it, BITS transfers include an on-screen progress bar that you can use to monitor the download.
Figure 4. Here’s how to perform a BITS transfer using PowerShell.