Click here to Skip to main content
13,904,766 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 24 Nov 2014
Licenced CPOL

PowerShell: Http Get/Post

, 24 Nov 2014
Rate this:
Please Sign up or sign in to vote.
How to use PowerShell to carry out REST operations such as GET/POST

This is another article in my on going learning/experimenting with PowerShell. This time, I will show how you can use PowerShell to carry out REST operations such as GET/POST.

Now there may be some amongst you, who go why didn’t you just use WGet, which is a downloadable thing, and I could have indeed used that, except for the part that the machine I need to run this on, is so locked down that I can only use things that are already installed. So raw PowerShell it is.

Here is the relevant PowerShell code, which allows 3 parameters to control the script:

  • Target: The url
  • Verb: This is the http verb, GET, PUT, etc.
  • Content: This could be some content when doing a POST request for example

It just makes use of some very standard .NET classes namely WebRequest.

    [string] $target,
    [string] $verb,      
    [string] $content
write-host "Http Url: $target"
write-host "Http Verb: $verb"
write-host "Http Content: $content"
$webRequest = [System.Net.WebRequest]::Create($target)
$encodedContent = [System.Text.Encoding]::UTF8.GetBytes($content)
$webRequest.Method = $verb
write-host "UTF8 Encoded Http Content: $content"
if($encodedContent.length -gt 0) {
    $webRequest.ContentLength = $encodedContent.length
    $requestStream = $webRequest.GetRequestStream()
    $requestStream.Write($encodedContent, 0, $encodedContent.length)
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
if($resp -ne $null) 
    $rs = $resp.GetResponseStream();
    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
    [string] $results = $sr.ReadToEnd();
    return $results
    exit ''

Here is an example usage (assuming the above script is saved as http.ps1 somewhere):

Http.ps1 - target "" -verb "GET"

which would give the following sort of output:

Anyway that’s it for now, hope this helps!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.


I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 25 Nov 2014
Article Copyright 2014 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid