Chapter 12. Internet-Enabled Scripts


Although PowerShell provides an enormous benefit even when your scripts interact only with the local system, working with data sources from the Internet opens exciting and unique opportunities. For example, you might download files or information from the Internet, interact with a web service, store your output as HTML, or even send an email that reports the results of a long-running script.

Through its cmdlets and access to the networking support in the .NET Framework, PowerShell provides ample opportunities for Internet-enabled administration.

Download a File from an FTP or Internet Site


You want to download a file from an FTP location or website on the Internet.


Use the -OutFile parameter of the Invoke-WebRequest cmdlet:

PS > $source = ""
PS > $destination = "c:\temp\favicon.ico"
PS >
PS > Invoke-WebRequest $source -OutFile $destination


The Invoke-WebRequest cmdlet lets you easily upload and download data from remote web servers. It acts much like a web browser in that you can specify a user agent, a proxy (if your outgoing connection requires one), and even credentials.


If you require a solution that works with PowerShell version 2, use the DownloadFile() method of the System.Net.WebClient class from the .NET Framework.

While the Solution demonstrates downloading a file from a web (HTTP) resource, the Invoke-WebRequest cmdlet also supports FTP locations. To specify an FTP ...

Get Windows PowerShell Cookbook, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.