0

I have created the following C# code to run a powershell script:

static void Main(string[] args)
        {

            PowerShell ps = PowerShell.Create();
            string script = "";
            script = "Set-WinUserLanguageList -LanguageList en-US,de-DE,uk ";
            ps.AddScript(script);
            var result = ps.Invoke();

            Console.WriteLine(result);
            Console.ReadLine();
        }

If I run Set-WinUserLanguageList -LanguageList en-US,de-DE,uk from PowerShell, it is working fine and my windows language list changes in the defined order (EN,GER,UK). But if I run my C# code nothing happens.

I checked Google but couldn't find any vital difference...

Anyone has an idea what I have to change in my C# code to make it working?

Thanks!

mklement0
  • 382,024
  • 64
  • 607
  • 775
bluefox
  • 175
  • 3
  • 16
  • 1
    When you use `.AddScript()`, any errors that occur during execution of the PowerShell code do _not_ surface _as exceptions_ in your C# program; instead, you must examine the `.Streams.Error` collection (`.HadErrors` is a Boolean that indicates whether any errors occurred) - see [this answer](https://stackoverflow.com/a/69051991/45375) for details. – mklement0 Apr 25 '23 at 12:35

1 Answers1

1

Add -Force flag to the command

i.e Set-WinUserLanguageList -Force -LanguageList en-US,de-DE

Mahesh Anakali
  • 344
  • 1
  • 8