Matt McManis Matt McManis - 2 months ago 30
C# Question

Resize PowerShell Window through C# Process.Start()

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.

Answer Source

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";