Execute a SOAP request from Powershell

Web services are everywhere such as SOAP requests, WCF services and traditional ASMX asp.net web services.   I was working on a task recently we needed to call a SOAP service from a powershell script.  A good friend of mine passed this snippet to me.  If you want to test SOAP requests, using SOAP UI  This allows you to build soap requests.   Hope this helps.

These return XML objects with the server’s response.

function Execute-SOAPRequest
        [Xml]    $SOAPRequest,
        [String] $URL
        write-host “Sending SOAP Request To Server: $URL”
        $soapWebRequest = [System.Net.WebRequest]::Create($URL)

        $soapWebRequest.ContentType = “text/xml;charset=`”utf-8`””
        $soapWebRequest.Accept      = “text/xml”
        $soapWebRequest.Method      = “POST”
        write-host “Initiating Send.”
        $requestStream = $soapWebRequest.GetRequestStream()
        write-host “Send Complete, Waiting For Response.”
        $resp = $soapWebRequest.GetResponse()
        $responseStream = $resp.GetResponseStream()
        $soapReader = [System.IO.StreamReader]($responseStream)
        $ReturnXml = [Xml] $soapReader.ReadToEnd()
        write-host “Response Received.”

        return $ReturnXml

function Execute-SOAPRequestFromFile
        [String] $SOAPRequestFile,
        [String] $URL
        write-host “Reading and converting file to XmlDocument: $SOAPRequestFile”
        $SOAPRequest = [Xml](Get-Content $SOAPRequestFile)

        return $(Execute-SOAPRequest $SOAPRequest $URL)


Steve Schofield
Microsoft MVP – IIS

One thought on “Execute a SOAP request from Powershell”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: