Send SOAP message from Powershell

Today I want to give you example how to send SOAP message from Powershell.
Nowadays is very popular that most of the applications have API interface for external applications to be used.
New applications use REST web service instead of SOAP, because JSON syntax is simpler than XML.
However sometimes there is a need to collect data from older, SOAP based applications and today I wanna show you how to do it.

In first way $SOAPRequest variable should be built in correct way.
To find definitions of all SOAP methods WSDL (Learn more about WSDL) should be checked.

$SOAPRequest = @"
<soapenv:Envelope xmlns:soapenv="" xmlns:itp="">   <soapenv:Header/>

Once it is filled correctly $URL should be provided.
In my example I used certifiacte to authenticate, however script can be modified and credentials authentication can be used.
Modification of these two lines will be needed.

$soapWebRequest.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$soapWebRequest.UseDefaultCredentials = $true

It’s important to call proper name of the SOAP action on the webservice. For that in the $soapWebRequest.Headers we should add proper method.
Remember also to modify method type. In my case I used POST but can be any other, double check WSDL 😉

[String] $URL = 'Here paste URL to SOAP API'

$cert = Get-ChildItem -Path Cert:\LocalMachine\My | where-Object {$_.Subject -like 'Subject of certificate'} 

if($cert -ne $null)

        # Sending SOAP Request To Server 
        $soapWebRequest = [System.Net.WebRequest]::Create($URL) 
        $soapWebRequest.Headers.Add("SOAPAction","Provide method name of the API")
        $soapWebRequest.ContentType = "text/xml;charset=utf-8" 
        $soapWebRequest.Accept      = "text/xml" 
        $soapWebRequest.Method      = "POST"
        $soapWebRequest.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
        $soapWebRequest.UseDefaultCredentials = $true 
        #Initiating Send
        $requestStream = $soapWebRequest.GetRequestStream() 
        #Send Complete, Waiting For Response.
        $resp = $soapWebRequest.GetResponse() 
        $responseStream = $resp.GetResponseStream() 
        $soapReader = [System.IO.StreamReader]($responseStream) 
        $ReturnXml = [Xml] $soapReader.ReadToEnd() 

        Throw $ReturnXml.Envelope.InnerText

    $Return = $ReturnXml.Envelope.InnerText

    $Return = "Certificate not found"

$SOAPRequest = $SOAPRequest.OuterXML

I hope that it will be usefull for some of you 😉

2 thoughts on “Send SOAP message from Powershell

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.