Matt McManis Matt McManis - 3 days ago 7
C# Question

Resize PowerShell Window through C# Process.Start()

I launch PowerShell and commands through C#.

Using Windows 10.

Method 1

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



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


I've tried
-nologo -noprofile -command

I also tried using this window resize

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:


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