Powershell to Exchange 2013 – Restricted language mode error


I’m working on a C# web service that will be deployed on an Exchange 2013 server. This service will be responsible for running powershell commands to configure Exchange.

I connect via a runspace created like this

Using this runspace I am able to run basic powershell commands on the server.

But running more complicated commands gives me errors (this does work if run directly through powershell)

After searching I found that I might have to register a new PSSessionConfiguration and make sure the scripts are running under PSLanguageMode = FullLanguage. See this post

I tried to do this, but as soon as I change the shellUri to http://schemas.microsoft.com/powershell/MyConfigName I get the following error.

Using the following shelllUri gave me the same error http://schemas.microsoft.com/powershell/Microsoft.Powershell

This led me to try the following directly through powershell on the exchange server

Like from the C# code, I can only use the Microsoft.Exchange configuration, but this configuration doesn’t exist according to Get-PSSessionConfiguration, and the configurations that do exist in that list won’t work.

I am now wondering how to add a configuration with FullLanguage that I can use when calling powershell from code.
I might also be completely wrong, and my issue is not related to PSSessionConfigurations at all, but then I still wonder why I can’t see the Microsoft.Exchange configuration anywhere.


I ended up getting support from Microsoft and they provided the following solution which works. Instead of connection via a remote powershell session, it is possible to connect to the local powershell and then import the remote session. Doing this fixes the issue.

Error handling not included in example below


Powershell to Exchange 2013 – Restricted language mode error by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply