The Invoke-WebRequest is a very useful and cool Cmdlet in my opinion. This Cmdlet enables you to download files from the internet. Why the need to use Invoke-WebRequest just to download a file? I can name some reasons but the main reason is because I perform repeatable tasks in my daily job as a Technical Consultant. So I’m striving to work as efficiënt as possible. As most of us experience: time is always an issue! For example: what if you need to perform 5 identical downloads every day on different machines every time to basically the same folder? I won’t do that manually everytime but I’m going to find or create a solution for it in order to simplify and automate the task and gain more time in the long run.
So, the first thing that came to my mind: Powershell.
I did some research on Technet. On Microsoft Technet I found this Cmdlet and more information about it ofcourse. After reading quickly through the page I didn’t saw any examples… I got unpatient :) So I immediately started the PowerShell ISE and just typed in the Cmdlet.
The Invoke-WebRequest cmdlet asked me an Uri. That sounds logical. So I entered the URI to the .NET Framework 4.6 Offline installer, because I needed it to download. I quickly found it that it didn’t produced the result I wanted. My goal was to download the file and to save it to my Desktop. So once again I checked the documentation on Technet. The Outfile parameter saves the output to a file. So after specifying a destination filename it all worked.
At the end I created a small script with some extra things like dynamic constructing the path to the current desktop and it resulted in a small script I now use a lot. I will share it so you may benefit or learn from it too:
PowerShell example: Download .NET Framework 4.6:
$DownloadURL="https://download.microsoft.com/download/C/3/A/C3A5200B-D33C-47E9-9D70-2F7C65DAAD94/NDP46-KB3045557-x86-x64-AllOS-ENU.exe"
$Path_Desktop="$env:USERPROFILE\Desktop"
$Path_DotNetSetup46="$Path_Desktop\NDP46-KB3045557-x86-x64-AllOS-ENU.exe"
# Download: Microsoft .NET Framework 4.6 (Offline Installer) for Windows Vista SP2, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2008 SP2 Windows Server 2008 R2 SP1, Windows Server 2012 and Windows Server 2012 R2. URL: https://www.microsoft.com/en-US/download/confirmation.aspx?id=48137
Invoke-WebRequest -Uri $DownloadURL -OutFile $Path_DotNetSetup46 -Verbose
Write-Output ".NET Framework 4.6 download completed."