Skip to content

How to call a RESTful web API using PowerShell?

Needless to say that my love for PowerShell grows every time I make use of it. I had a good discussion with one of my fellow colleagues some time back on whether PowerShell should be considered a scripting language or a programming language. It’s for sure that it is NOT a programming language. Having said that, just to classify it as scripting language (remember VB script?) will not be justice with it’s capabilities.

“PowerShell is a true DevOps language.”

It has the right tools in it’s kitty which not only helps the core developers but also the day-to-day Administrators. And I am still talking about the OLD WORLD where we had those distinctions. Now, let’s come back to 2015 where even Microsoft promotes to deploy right from your Visual Studio.

Anyway, I should come back to the intention of this post before I drift further. It’s about using PowerShell to call RESTful APIs using PowerShell.

Let’s take an example where I want to call a service which sends an email accepting a JSON object with To address, From address, Subject, Body etc. for creating a mail message object. Thanks to Postman, testing your APIs are more easier than ever as you’t have to write the calling part anymore.


Having, my API ready I wanted to schedule this API to called at a certain interval. Yes, I miss those Azure Web Jobs when running on IIS.

However, the good old Task Scheduler is still handy to create a task which calls a PowerShell script. The script looks something like this:

It uses Invoke-RestMethod. The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that returns richly structured data. However, the additional parameters such as Body, ContentType etc. really make it powerful to use it. If you look at the script, I defined a normal PowerShell Hash Table with a mapping of key and values. And later using ConvertTo-Json to convert hash table as a JSON object.

Published inUncategorized

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *