Centre PowerShell forms

The .NET Framework class Form represents a window or dialog box that makes up an application’s user interface. In other words this is how we can build a GUI interface to an application with forms and dialog boxes.


The StartPosition property of the form is a way of being able to specify the initial starting position of a form. So this is useful if we wish to place the form in the centre of the screen. Use the following code to achieve this:


#Sets the starting position of the form at run time.
$CenterScreen = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form1.StartPosition = $CenterScreen;


The enumeration type System.Windows.Forms.FormStartPosition has several members to choose from and the one we’re interested in is CenterScreen. From the documentation, the description for this member is:


The form is centered on the current display, and has the dimensions specified in the form’s size.


If your PowerShell session does not seem to know about forms, then import the assembly.

[reflection.assembly]::loadwithpartialname(“System.Windows.Forms”) | Out-Null


The following loop will show the other members of System.Windows.Forms.FormStartPosition.


foreach ($m in [System.Enum]::GetValues([System.Windows.Forms.FormStartPosition]))
{ Write-Host ([int]$m): $m }


See also

How to Create and Use Enums in Powershell


Keywords: powershell form center centre start position





This entry was posted in powershell and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s