Automating Software Downloads with PowerShell
PowerShell provides multiple ways to push new software to targeted systems.
- By Adam Bertram
When automating a tedious task or building an entire automation framework, a frequent problem arises when you must install software. Before you can install the software you must first somehow get the bits onto the machine you'll like to install it on. Luckily for us, this is easily done using a couple different methods in PowerShell.
In this article, I will be explaining a few different methods to download software from the Internet onto a Windows computer.
Background Intelligent Transfer Service (BITS)
BITS is a service that's been around in Windows for a while now. It's a resilient file transfer service performed over HTTP or HTTPS that allows you to perform file transfer as jobs. It also supports features like throttling, queuing and a few other useful features. PowerShell has built-in support for creating BITS jobs by its Start-BitsTransfer cmdlet.
By using the Start-BitsTransfer cmdlet, you can specify the URL where the software file is located, the local file path and a few other options to easily download software onto your local computer.
At a minimum, to download files with the Start-BitsTransfer service, you'll need two different parameters; the URI of the remote file and the local path where the file will be stored. To do this, you'll open up your PowerShell console as administrator and run Start-BitsTransfer as follows:
PS> Start-BitsTransfer –Source 'https://raw.githubusercontent.com/adbertram/Random-PowerShell-Work/master/IIS/New-IISWebBinding.ps1' -Destination 'C:\New-IISWebBinding.ps1'
Above, I am downloading a PowerShell script from Github and placing into the root of C. Once it starts, you will see a progress bar and when finished, your file will be placed in the destination.
Another method you can use to download software is through the .NET Webclient object. This is an object that has a method called DownloadFile() that allows you to specify a URI for a file located on a webserver and the file path where you'd like the file to be downloaded to.
Since this method does not have an existing cmdlet already, we'll have to instantiate a .NET object and call the DownloadFile() method directly. To do this, we need to use the New-Object cmdlet and specify the type.
Here I am performing the same action as before with BITS although this time I'm using the WebClient object.
PS> $webClient = New-Object –TypeName System.Net.WebClient
PowerShell Package Manager
Although not capable of pointing at any URI, PowerShell Package Manager (OneGet) is another great way to automate software downloads. Built into Windows 10, this options allows you to look at specific remote providers and to download packages from them.
For example, by default, these are the providers that come up for me in Windows 10.
When I run, Get-Package, I can see that I have 343 packages available to me. Let's pick one called Adobe Air.
I now see that I have Adobe Air available to me. Now, instead of simply downloading the software, I can easily pipe this to the Install-Package cmdlet to get it installed silently. No need for worrying about installer syntax and figuring out all of the switches needed to get it installed.
Get-Package -Name 'Adobe Air' | Install-Package
As you can see, PowerShell provides you with a few different options to automate software downloads (and installs). If you need to reference software from various URIs on the Internet, I'd suggest you either use BITS or the WebClient object. However, if you're able to get your software into a PowerShell package provider, it is much easier to manage there and to get installed.
Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.