krono-matrixer
krono-matrixer

Reputation: 11

C# in PowerShell: Language Mode Not Supported

I am trying to get a very simple script to work in PowerShell, and it keep popping back with the following error:

Add-Type : Cannot add type. Definition of new types is not supported in this language mode.
At C:\Users\jdkin_000.ATHENA\cs_init.ps1:16 char:1
+ Add-Type -TypeDefinition $Source -Language CSharp

Here is the script:

$Source = @"
using System;

namespace cs1
{
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Hello world!");
        }
    }
}
"@

Add-Type -TypeDefinition $Source -Language CSharp

Any suggestions on how to get this script to work on the exact device that is throwing back the error. The script and similar scripts work on other machines, but when the script contains C#, this machine just doesn't want to play fair.

Upvotes: 1

Views: 7220

Answers (2)

Anton Krouglov
Anton Krouglov

Reputation: 3399

Powershell also may switch to ConstrainedLanguage mode when there is no space available on system drive.

Upvotes: 1

Sako73
Sako73

Reputation: 10137

Try running this:

$ExecutionContext.SessionState.LanguageMode

Here is the link explaining the LanguageMode. I assume you are in a mode that restricts the ability to define new types.

https://technet.microsoft.com/en-us/library/dn433292.aspx

Edit: Based on the Windows RT 8.1 OS, ( Windows RT Powershell (PermissionDenied) on New-Object) It doesn't appear that it can be done.

Upvotes: 2

Related Questions