Sign up for our newsletter.

I agree to this site's Privacy Policy.

Prof. Powershell

PowerShell 3 Web of Wow, Part 2

In part 2 of this series, we take a look at cmdlet RestMethod.

Last time we looked at one of the new PowerShell v3 cmdlets for working with web data, Invoke-WebRequest. This week we'll look at a cmdlet that I think you'll find even easier to use, Invoke-RestMethod. REST, which stands for Representational State Transfer is being used more and more to deliver web-based data.

But don't worry, I'm not going to turn this into a .NET programming lesson. That's the beauty of using a cmdlet; it does all the heavy lifting. You just have to use the results. Here's an easy demonstration.

PS C:\> $url = ""
PS C:\> $data = invoke-restmethod $url

The URL is to the RSS feed for this column. Here's what $data looks like (see Fig. 1):

Invoke-WebRequest cmdlet

Figure 1. Example of $data. (Click image to view larger version.)

Behind the scenes, Invoke-Restmethod creates an XML object.

PS C:\> $data[0].gettype().name

This makes it very easy to get the information we want.

PS C:\> $data | Select PubDate,Link,Title | out-gridview -title "Prof. PowerShell"

You can see the results in Figure 2:

Invoke-WebRequest cmdlet

Figure 2. Results from Invoke-Restmethod. (Click image to view larger version.)

One thing to be careful of, depending on the results you get from Invoke-Restmethod, is that most properties will be strings. In this example this means if you try to sort on the PubDate property, you won't get the results you expect. The solution is to treat the property as a DateTime object.

PS C:\> $data | Select @{Name="Published";Expression={$_.PubDate -as
[datetime]}},Title,Link | where Published -gt "11/01/2012" | Sort Published | format-

Published : 11/6/2012 7:14:30 PM
title : HTML Bits and Pieces, Part 1
link :

Published : 11/13/2012 6:31:26 PM
title : HTML Bits and Pieces, Part 2
link :

Published : 11/27/2012 4:15:33 PM
title : HTML Bits and Pieces, Part 3
link :

Published : 12/4/2012 4:26:02 PM
title : A Better View of PowerShell Help
link :

Published : 12/11/2012 4:25:12 PM
title : Get Your PowerShell Object Properties In Order
link :

I created a new property called Published that is the PubDate property cast as a datetime object and then I filter out articles published since Nov. 1, 2012. This is a great cmdlet for building an RSS reporting tool in PowerShell.

But some web services can be a bit more interactive. A typical example that most people can relate to is Twitter. Sites, like Twitter, often provide a web-based API that you can query. Sometimes you need an API key and authorization token, but I leave those examples for you. This is pretty straight forward.

PS C:\> $url =

The query says find all tweets in the English language with "PowerShell" and return 10 results per page. Now to invoke the query.

PS C:\> $data = Invoke-RestMethod $url

You can see the $data object in Figure 3:

Invoke-WebRequest cmdlet

Figure 3. PowerShell query results. (Click image to view larger version.)

PowerShell should take the response and create a custom object. The information I want is in the results property. It looks like a collection so I'll peek at the first one to figure out what it looks like (see Figure 4).

PS C:\> $data.results[0]

Invoke-WebRequest cmdlet

Figure 4. PowerShell data results. (Click image to view larger version.)

Excellent! Now I can get the information I want from the results.

PS C:\> $data.Results | Select @{Name="Datetime";Expression={$_.created_at -as
[datetime]}},@{Name="From";Expression={"{0} ({1})" -f
{0}/status/{1}" -f $_.from_user,$ }} | Out-GridView -Title "PowerShell Tweets"

Figure 5 displays the results:

Invoke-WebRequest cmdlet

Figure 5. Information extracted from data results. (Click image to view larger version.)

Invoke-RestMethod is a powerful tool and I've only scratched the surface. Please find some time to look through full help and examples. In the last part of this series we'll look at one final way of interacting with web-based services.

More on this topic:

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog, on Twitter at and on Google Plus (http:/

comments powered by Disqus

Reader Comments:

Mon, Feb 10, 2014

http://w DOT DOT DOT nline_casino.php DOT http://w DOT

Sun, Feb 2, 2014 DOT DOT http://w DOT DOT DOT DOT

Thu, Jan 30, 2014 DOT DOT orex-online.html DOT http://y DOT DOT

Sat, Jan 25, 2014

http://w DOT http://w DOT http://w DOT http://y DOT DOT orex-online.html DOT

Tue, Jan 21, 2014

http://w DOT http://w DOT http://w DOT http://w DOT http://f DOT http://w DOT

Sat, Jan 18, 2014 Kailin http://w DOT DOT http://w DOT http://w DOT

http://w DOT DOT http://w DOT http://w DOT DOT DOT

Wed, Nov 6, 2013 Janai affordable car insurance quotes DOT

Sun, Nov 3, 2013 Dede #19 My boyfriend and I went to an art muuesm in Montreal and one of the pieces was literally just a wooden box. Just a plain, not painted, wood freaking box.

#19 My boyfriend and I went to an arthttp://r DOT in Montreal and one of the pieces was literally just a wooden box. Just a plain, not painted, wood freaking box.

Mon, Sep 30, 2013 Keys small business insurance cash back credit card DOT DOT

Fri, Sep 27, 2013 Kayleen best credit card cash back DOT DOT

Thu, Sep 26, 2013 Summer credit card rewards DOT

Thu, Sep 19, 2013 Delores buy viagra on line cialis DOT DOT

Fri, Sep 13, 2013 Marsue car insurance quotes low rate credit cards DOT DOT

Sun, Jul 28, 2013 Kathreen car insurance cheap cheap car insurance breakdown DOT DOT hould-Women-Pay-Less-Than-Men-for-Their-Auto-Insurance.html

Fri, Jul 26, 2013 Janaye car insurance quotes cheap auto insurance free DOT hould-Women-Pay-Less-Than-Men-for-Their-Auto-Insurance.html DOT

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above