1

I launch PowerShell and commands through C#.

Using Windows 10.

Method 1

Process.Start("powershell.exe",
    "Write-Host \"Hello, world!\"; timeout 30"
    );

Method 2

using (Process ps = new Process())
{
    ps.StartInfo.UseShellExecute = false;
    ps.StartInfo.Verb = "runas";
    ps.StartInfo.CreateNoWindow = false;
    ps.StartInfo.RedirectStandardOutput = false;
    ps.StartInfo.FileName = "powershell.exe";
    ps.StartInfo.Arguments = "Write-Host \"Hello, world!\"; timeout 30";

    ps.Start();
    ps.WaitForExit();
    ps.Close();
}

Problem

However the window size is much larger than when launching PowerShell normally.

Are there any commands or launch parameters that can lower the window size?

Instead of forcing a new width and height, I'd like to have it revert to it's original size so it doesn't affect other versions of PowerShell in Windows 7.

Left: C# Process.Start().
Right: Normal PowerShell

Solutions

I've tried -nologo -noprofile -command.

I also tried using this window resize
https://stackoverflow.com/a/6485074/6806643

It works in a Console Application, but I can't get it to work in WPF.
Also PowerShell will exit if I try to resize the width by hand.

Matt McManis
  • 4,475
  • 5
  • 38
  • 93
  • 1
    Possible duplicate of [How to set the height of a window using c#?](https://stackoverflow.com/questions/6484567/how-to-set-the-height-of-a-window-using-c) or you can try something looks like this `ps.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;` – George Alexandria Aug 12 '17 at 20:41
  • Take a look at `gci HKCU:/console/%SystemRoot%_Sys*` –  Aug 12 '17 at 20:48
  • @GeorgeAlexandria It works in a Console Application, but I can't get it to work in WPF. It adjusts the Window Size, but the font size is still large. – Matt McManis Aug 12 '17 at 21:04
  • I suggest to add this info to your question: resize window and font. – George Alexandria Aug 12 '17 at 21:10
  • @GeorgeAlexandria I've updated the question with some new info. – Matt McManis Aug 12 '17 at 21:21
  • Lots of details about how the console windows settings are stored and default: https://blogs.msdn.microsoft.com/commandline/2017/06/20/understanding-windows-console-host-settings/ – Richard Aug 13 '17 at 10:10

1 Answers1

2

The difference is because the shortcut in the start menu has it's own set of properties.

You could change the default settings in the registry to match the shortcut. They are in these locations:

HKEY_CURRENT_USER\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe
HKEY_CURRENT_USER\Console\%SystemRoot%_SysWOW64_WindowsPowerShell_v1.0_powershell.exe

Or you could just set it within PowerShell by adding to your arguments like this:

ps.StartInfo.Arguments = "$Host.UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.Size (120, 50); Write-Host \"Hello, world!\"; timeout 30";
Patrick Meinecke
  • 3,963
  • 2
  • 18
  • 26
  • I'd like to use the Arguments, so it does not permanently change the user's registry. I tried your arguments but it did not seem to take effect. – Matt McManis Aug 12 '17 at 21:58
  • @MattMcManis Sorry, you need to change the values `120` and `50` in `(120, 50)` to the size you want. – Patrick Meinecke Aug 12 '17 at 22:05
  • That worked, I also added `$Host.UI.RawUI.BufferSize`. One more thing, do you know how to adjust the font size? I tried this ISEOptions but it gives an error `The property 'FontSize' cannot be found on this object.`. https://learn.microsoft.com/en-us/powershell/scripting/the-iseoptions-object?view=powershell-6#fs – Matt McManis Aug 12 '17 at 22:30
  • @MattMcManis I don't think there is a way without using win32 APIs, which would be way too verbose to load in via process arguments. FYI the ISE variable only exists in the ISE's terminal. It also uses a custom host, so you wouldn't be able to use the same method. – Patrick Meinecke Aug 12 '17 at 22:41