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 a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.