How PowerShell Can Download Files from the Internet
The purpose of this page is two fold; firstly, the practical task of downloading a file. Secondly, to explore one of the many uses of the New-Object cmdlet.
Windows PowerShell New-Object Topics
- Download a File from the Internet Using PowerShell
- Help with New-Object’s Parameters
- Research New-Object Methods and Properties
- PowerShell New-Object in Action
- PowerShell New Item
New-Object is one of the most versatile cmdlets, in this instance it creates a WebClient. The source is .Net Framework, and as we can see, this object has a method called .DownloadFile, which completes the task thanks to the $Url and $LocalPath variables.
# PowerShell script to download a file from the internet
$Url = "http://tinyurl.com/axthvc8"
$LocalPath = "C:\Downloads\Animals.themepack"
$Wget = New-Object System.Net.WebClient
Note 1: The .DownloadFileAsync method needs to know the internet URL and also the local path to paste the destination copy of the file.
$Wget = New-Object System.Net.WebClient
$Wget | Get-Member
Note 3: System.Net.WebClient also has a method called plain DownloadFile; using this parameter seems to take longer, or at least to tie up the console, whereas DownloadFileAsync is more of a background operation.
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.
Help New-Object -full
Note 2: -ComObject is one of the most interesting parameters.
Whereas we used a System.Net object in the script above, you can also access the -ComObjects. Examples of COM (Component Object Model) are Windows Script Host (WSH) and ActiveX applications such as Internet Explorer.
New-Object -ComObject WScript.Shell
New-Object -ComObject WScript.Network
New-Object -ComObject Scripting.Dictionary
New-Object -ComObject Scripting.FileSystemObject
New-Object is a concept. This particular PowerShell cmdlet enables you to build your own object, as we will see, you can combine properties of different underlying objects to create a new custom object.
This script uses two WMI classes to create the building blocks. Then look out for
# New-Object creates custom object.
#PowerShell New-Object Example
# Preamble – Operating System Properties
$OS = Get-WmiObject -Class Win32_OperatingSystem
$Boot = [Management.ManagementDateTimeConverter]::ToDateTime($OS.LastBootUpTime)
# Preamble – Bios Properties
$Bios = Get-WmiObject Win32_BIOS
# New-Object creates custom object
$Custom = New-Object –TypeName PSObject
$Custom | Add-Member –MemberType NoteProperty –Name BootTime –Value $Boot
$Custom | Add-Member –MemberType NoteProperty –Name OSBuild –Value $OS.BuildNumber
$Custom | Add-Member –MemberType NoteProperty –Name BiosMake –Value $Bios.Manufacturer
$Custom | Format-Table BiosMake, OSBuild, BootTime -AutoSize
Note: Observe and trace how $Custom is built of properties contained by two other objects (Operating System and Bios).
Research Other PowerShell ‘Object’ Cmdlets
You can use Get-Command to investigate additional members of PowerShell's 'Object' family. Simply append the -Noun parameter. Incidentally, you could also try -Verb New.
Get-Command -Noun Object
Summary of PowerShell New-Object
Scenario: You wish to download a file from the internet, or a network server. Solution: Call for PowerShell’s New-Object; create a system.net.webclient type of object. Specify the web url, and the local path in the .DownloadFile method.
If you like this page then please share it with your friends
See more PowerShell examples of ‘Object’ cmdlets
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.