c# programatically exporting transport rule RRS feed

  • Question

  • Exporting transport rule programmatically

    Hi ,

    I am trying to export transport rule programmatically.

    My code works for simple commands like

    get-exchangeserver, get-TransportRule etc but if I try to give command importing rules to some file it gives error that the given command is not recognized as cmdlet, script….

    My code is as below ( I have tried giving both windows authentication or user ‘s authentication who is exchange admin)

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(   serverUri,





                    using (Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo))


                        PowerShell psh = PowerShell.Create();

                        psh.Runspace = rs;


    psh.AddCommand("$file = Export-TransportRuleCollection -ExportLegacyRules" +"Set-Content -Path " + @"C:\LegacyRules3.xml" + "-Value file.FileData -Encoding Byte");



    I am using c# and exchange server 2010 PowerShell version is 1.0 the remoite os is winodws 7.

    Any idea what correction I should do?



    Monday, April 15, 2013 8:17 AM


  • Hi Laeeq Qazi,

    I tried code from below link:

    and now my code works for remote connection, but now I am trying for running the code directly on exchnag server and facing issue that is getting same error that the given command is not recognized as cmdlet, script….

    I have set ExecutionPolicy unrestricted still facing issue, below is not sure what is the problem,

    Let me know if you have sample code  to call script through c# exchange server(i.e not by remote method)



    • Marked as answer by ABBhagwat Thursday, April 18, 2013 8:18 AM
    Thursday, April 18, 2013 6:50 AM

All replies