PowerShell New-Object

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

 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
$Wget.DownloadFileAsync($Url, $LocalPath)

Note 1:  The .DownloadFileAsync method needs to know the internet URL and also the local path to paste the destination copy of the file.

Research New-Object Methods and Properties

Clear-Host
$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.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

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.

Download your free copy of WMI Monitor

Help with New-Object’s Parameters

Clear-Host
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

See more examples of PowerShell’s -ComObject »

PowerShell New-Object in Action

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.

#Preamble
This script uses two WMI classes to create the building blocks.  Then look out for
# New-Object creates custom object.

#PowerShell New-Object Example
Clear-Host
# 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.

Clear-Host
Get-Command -Noun Object

See PowerShell use New-Object to create a shortcut »

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

PowerShell Tutorials  • Syntax  • PowerShell Where-Object  • Foreach-Object  • Free WMI Monitor

  • New-Item  • Remove-Item  • ItemProperty  • PowerShell New Object   • Sort-Object   • Group-Object

PowerShell Splatting  • Select-String  • -replace string  • Compare-Object  • Measure-Object

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.