Table of content
- Introduction
- What we actually trying to do
- Microsoft Windows PowerShell & Microsoft .Net Library
- Microsoft.Net Library – Namespace(s)
- System.Windows.Forms
- System.Collections
- System.Drawing
- System.Reflection
- System.IO
- System.Xml
- Get Help
- Microsoft.Net Library – Namespace(s)
- Conclusion
- Reference
- History
Introduction
The objective of this article (Part-4) is to introduce you how to integrate Microsoft Windows PowerShell with the Microsoft .Net library; I hope you know that one of the great features of PowerShell is the integration with the Microsoft .Net Framework library.
In this article we will discuss about the integration of Microsoft Windows PowerShell with Microsoft .Net Framework library.
What we actually trying to do
We will try to demonstrate some common use .Net class in PowerShell script. How to write custom class in PowerShell, This is a series of articles, So If you missed the previous articles, nothing to worry about that. I would like to request you to please read the previous par(s) from the link below:
A Quick guideline for Microsoft Windows PowerShell
- A Quick guideline for Microsoft Windows PowerShell Part-1.
- A Quick guideline for Microsoft Windows PowerShell Part-2.
- A Quick guideline for Microsoft Windows PowerShell Part-3.
If you already familiar with the previous topic as well so why are we waiting for let’s start:
Microsoft Windows PowerShell & Microsoft .Net Library
I hope that you have all the basic knowledge about Microsoft Windows PowerShell & I assume that you have already familiar with Microsoft .Net library. So we will work on item listed below:
Microsoft.Net Library – Namespace(s):
The .NET Framework is a software framework that contains the Common Language Runtime and Base Class Libraries that provide much of the functionality. There are several versions of the .NET Framework.
PowerShell is compatible with versions 2.0, 3.0, and 3.5.System.Windows.Forms. In this section we will discuss few of them are listed below:
- System.Windows.Forms
- System.Drawing
- System.Collections
- Etc
For more information can be found at this link.
System.Windows.Forms
The System.Windows.Forms namespace contains classes for creating Windows-based applications that take full advantage of the rich user interface features available in the Microsoft Windows operating system.
For more information can be found at this link.
Well, We are now write a PowerShell script which will display a Windows Form with a display message & and Button with exit event as well. The sample script is given below:
Example:
$psScriptName = "ScriptWinForm.ps1"
$psScriptAuthor = "Md. Marufuzzaman"
$psScriptDate = "19/02/2009"
## .Net Namespace
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
## Creating instance of System.Windows.Forms.Form
$objForm = New-Object System.Windows.Forms.Form
## Set Form caption
$objForm.Text = "The Code Project"
##Set window size
$objForm.Size = New-Object System.Drawing.Size(350,150)
## Set window start position
$objForm.StartPosition = "CenterScreen"
## Add event for key press
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,40)
$objLabel.Text = "I will not say I have failed 1000 times; " +
"I will say that I have discovered 1000 " +
"ways that can cause failure – Thomas Edison."
$objForm.Controls.Add($objLabel)
## Creating System.Windows.Forms.Form.Button object
$OkButton = New-Object System.Windows.Forms.Button
## Set the button location / position
$OkButton.Location = New-Object System.Drawing.Size(150,75)
## Set button size
$OkButton.Size = New-Object System.Drawing.Size(75,23)
## Set button text
$OkButton.Text = "OKay"
## Set button click event
$OkButton.Add_Click({$objForm.Close()})
## Add the button into the From
$objForm.Controls.Add($OkButton)
$objForm.Topmost = $True
$objForm
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
The figure – A.

The figure – A is the output of the above PowerShell script.
System.Drawing
The System.Drawing namespace provides access to GDI+ basic graphics functionality. More advanced functionality is provided in the System.Drawing.Drawing2D, System.Drawing.Imaging, and System.Drawing.Text namespaces.
For more information can be found at this link.
Well, We are now write a PowerShell script which will focus on the Pen and SolidBrush objects.The GDI+ provides a range of other drawing objects. The following example creates SolidBrush and Pen objects which will perform drawing operations:
Example:
$psScriptName = "ScriptSystem.Drawing.ps1"
$psScriptAuthor = "Md. Marufuzzaman"
$psScriptDate = "19/02/2009"
## .Net Namespace
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
## Creating instance of System.Windows.Forms.Form
$objForm = New-Object System.Windows.Forms.Form
## Set Form caption
$objForm.Text = "The Code Project"
##Set window size
$objForm.Size = New-Object System.Drawing.Size(250,150)
## Set window start position
$objForm.StartPosition = "CenterScreen"
## Add event for key press
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
## Creating System.Windows.Forms.Form.Button object
$OkButton = New-Object System.Windows.Forms.Button
## Set the button location / position
$OkButton.Location = New-Object System.Drawing.Size(150,75)
## Set button size
$OkButton.Size = New-Object System.Drawing.Size(75,23)
## Set button text
$OkButton.Text = "OKay"
## Set button click event
$OkButton.Add_Click({$objForm.Close()})
$formGraphics = $objForm.createGraphics()
$objForm.add_paint(
{
$myBrush = New-Object System.Drawing.SolidBrush green
$myPen = New-Object System.Drawing.Pen red
$myPen.color = "red"
$myPen.width = 10
$Point1 = new-object Drawing.Point 10, 10
$Point2 = new-object Drawing.Point 100, 30
$Point3 = new-object Drawing.Point 170, 10
$Point4 = new-object Drawing.Point 200, 60
$formGraphics.DrawBezier($myPen, $Point1, $Point2, $Point3, $Point4)
}
)
## Add the button into the From
$objForm.Controls.Add($OkButton)
$objForm.Topmost = $True
$objForm
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
The figure -B .

The figure – B is the output of the above PowerShell script.
