24/7/365 Support

Windows

Download a Web Page from the Internet

Problem

You want to download a web page from the Internet and work with the content as a plain string.

Solution

Use the DownloadString() method from the .NET Framework’s System.Net.WebClient class to download a web page or plain text file into a string.

PS >$source = "http://blogs.msdn.com/powershell/rss.xml" PS > PS >$wc = NewObject System.Net.WebClient PS >$content = $wc.DownloadString($source)

Discussion

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 longrunning script.

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

Use Excel to Manage Command Output

Problem

You want to use Excel to manipulate or visualize the output of a command.

Solution

Use PowerShell’s ExportCsv cmdlet to save the output of a command in a CSV file, and then load that CSV in Excel. If you have Excel associated with .CSV files, the InvokeItem cmdlet launches Excel when you provide it with a .CSV file as an argument.

Example 87 demonstrates how to generate a CSV containing the disk usage for sub directories of the current directory.

Import Structured Data from a CSV File

Problem

You want to import structured data that has been stored in a CSV file. This is helpful when you want to use structured data created by another program, or structured data modified by a person.

Solution

Use PowerShell’s ImportCsv cmdlet to import structured data from a CSV file.

For example, imagine that you previously exported an inventory of the patches applied to a preVista system by KB number:

cd $env:WINDIR GetChildItem KB*.log | ExportCsv c:\temp\patch_log.csv

Store the Output of a Command in a CSV File

Problem

You want to store the output of a command in a CSV file for later processing. This is helpful when you want to export the data for later processing outside PowerShell.

Solution

Use PowerShell’s ExportCsv cmdlet to save the output of a command into a CSV file. For example, to create an inventory of the patches applied to a system by KB number (on preVista systems):

cd $env:WINDIR GetChildItem KB*.log | ExportCsv c:\temp\patch_log.csv

Easily Import and Export Your Structured Data

Problem

You have a set of data (such as a hashtable or array) and want to save it to disk so that you can use it later. Conversely, you have saved structured data to a file and want to import it so that you can use it.

Solution

Use PowerShell’s ExportCliXml cmdlet to save structured data to disk, and the ImportCliXml cmdlet to import it again from disk.

For example, imagine storing a list of your favorite directories in a hashtable, so that you can easily navigate your system with a “Favorite CD” function. Example 85 shows this function.

Access Information in an XML File

Problem

You want to work with and access information in an XML file.

Solution

Use PowerShell’s XML cast to convert the plaintext XML into a form that you can more easily work with. In this case, the RSS feed downloaded from the Windows PowerShell blog:

PS >$xml = [xml] (GetContent powershell_blog.xml)

Like other rich objects, PowerShell displays the properties of the XML as you explore. These properties are child nodes and attributes in the XML, as shown by Example 81.

Structured Files in Windows PowerShell

In the world of textonly system administration, managing structured files is often a pain. For example, working with (or editing) an XML file means either loading it into an editor to modify by hand, or writing a custom tool that can do that for you. Even worse, it may mean modifying the file as though it were plain text while hoping to not break the structure of the XML itself.

Pages

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say