How to Call a WCF Service from Windows PowerShell

01. December 2013 PowerShell, WCF 1

You can call your WCF operation from PowerShell. What you need is to use New-WebServiceProxy cmdlet. New-WebServiceProxy will create a proxy object in order to communicate with the service. Look at the example below:

  • Line 1: declares a variable to hold the wsdl URI
  • Line 2: creates a New-WebServiceProxy object based on that URI and with -UseDefaultCredential credential
  • Line 8: calls the service operation and passes necessary arguments

Note that your arguments would not be the same as what is presented in the service operation:

  • For non-string arguments there will be another argument of type Boolean with the same name but with Specified
    postfix which determines whether you are providing this argument or not.

    That’s why on line 8, I passed “true” after the argument “4”. There is no such an argument in my operation contract.

  • If your operation has return value you cannot get the returned value in common ways as what we normally do in .NET. You need to pass a reference argument (plus its Specified Boolean reference argument) which will be holding the result.

    That’s the reason I have two [ref] arguments at the end. There is no such an argument in the operation contract definition.

To get the list of arguments you can use Get-Member cmdlet or if you are using PowerShell ISE you can use the IntelliSense it provides.


1 thought on “How to Call a WCF Service from Windows PowerShell”

  • 1
    Pinkesh on August 2, 2018 Reply

    My API works in c# but same not worked in PowerShell it gives Error : The request failed with HTTP status 400: Bad Request. I am passing two double value and it should return me answer. Please help me

    $url = “any.svc?wsdl”
    try {
    $myService = New-WebServiceProxy -Uri $url -Namespace “WebServiceProxy”

    $myService | Get-Member -Name TestforSUMO
    $result = 0.0
    $resultSp = “true”
    $myService.TestforSUMO(30.0, “true”,30.0,”true”,[ref] $result, [ref] $resultSp)
    write (“Answer is ” + $result)
    }
    catch [System.Net.WebException]{
    Write ([string]::Format(“Error : {0}”, $_.Exception.Message))
    }

Leave a Reply

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