Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / PowerShell

A Quick Guideline for Microsoft Windows PowerShell - Part 4

Rate me:
Please Sign up or sign in to vote.
4.70/5 (19 votes)
22 Aug 2010CPOL3 min read 67.2K   289   33   12
The objective of this article (Part 4) is to give you an introduction on how to integrate Microsoft Windows PowerShell with the Microsoft .NET library

Table of Contents

  • Introduction
  • What are 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
  • 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 Are we Actually Trying To Do

We will try to demonstrate some common use .NET class in PowerShell script and how to write a custom class in PowerShell. This is a series of articles, so if you missed the previous articles, nothing to worry. I would like to request you to please read the previous part(s) from the links below.

A Quick Guideline for Microsoft Windows PowerShell

If you are already familiar with the previous topic as well, what 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 are already familiar with Microsoft .NET library. So we will work on items 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 a few of them which are listed below:

  • System.Windows.Forms
  • System.Drawing
  • System.Collections
  • Etc.

More information can be found at this link.

System.Windows.Forms

The System.Windows.Forms namespace contains classes for creating Windows-based applications that takes full advantage of the rich user interface features available in the Microsoft Windows operating system.

More information can be found at this link.

Well, we will now write a PowerShell script which will display a Windows Form with a display message & a 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()
Figure A.

win32Form.jpg

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 System.Drawing.Drawing2D, System.Drawing.Imaging, and System.Drawing.Text namespaces.

More information can be found at this link.

Well, we will now write a PowerShell script which will focus on the Pen and SolidBrush objects. 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()
Figure B.

win32Graphics.jpg

Figure B is the output of the above PowerShell script.

System.Collections

The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.

More information can be found at this link.

Example

$psScriptName   = "ScriptSystem.Collection.ps1"
$psScriptAuthor = "Md. Marufuzzaman"
$psScriptDate   = "19/02/2009"

### Variable declaration ###
###  hash table ###
$DictionaryEntry =  new-object system.collections.DictionaryEntry; 
$myHashTable = new-object system.collections.hashtable 
### ArrayList ###
$myArrayList = new-object system.collections.ArrayList 
## Assign some values
$myHashTable = @{"" = 1; "Code Project" = 2; "Is" = 3; "Cool" = 4}
$myArrayList = @{"" = 1; "Code Project" = 2; "Is" = 3; "Cool" = 4}

foreach($entry in $myHashTable)
{
    write-host ($entry.Keys , $entry.Values)
}

foreach($entry in $myArrayList)
{
    write-host ($entry.Keys , $entry.Values )
}

Output

Code Project Is Cool  2 3 4 1
Code Project Is Cool  2 3 4 1

Get Help

The Get-Help cmdlet displays information about Windows PowerShell cmdlets and concepts. You can also use "Help {<cmdlet name> | <topic-name>" or "<cmdlet-name> /?". "Help" displays the help topics one page at a time. The "/?" displays help for cmdlets on a single page.

Syntax
  • Get-Help [[-name] <string>] [-component <string[]>] [-functionality <string[]>] [-role <string[]>] [-category <string[]>] [-full] [<CommonParameters>]
  • Get-Help [[-name] <string>] [-component <string[]>] [-functionality <string[]>] [-role <string[]>] [-category <string[]>] [-detailed] [<CommonParameters>]
  • Get-Help [[-name] <string>] [-component <string[]>] [-functionality <string[]>] [-role <string[]>] [-category <string[]>] [-examples] [<CommonParameters>]
  • Get-Help [[-name] <string>] [-component <string[]>] [-functionality <string[]>] [-role <string[]>] [-category <string[]>] [-parameter <string>] [<CommonParameters>]

References

  • Microsoft Development Network

Conclusion

I hope this might be helpful to you. Enjoy!

History

  • 23rd August, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
libenwei22-Nov-10 16:57
libenwei22-Nov-10 16:57 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman5-Oct-11 23:17
professionalMd. Marufuzzaman5-Oct-11 23:17 
GeneralMy vote of 5 Pin
Mohsiul Haque2-Nov-10 2:45
Mohsiul Haque2-Nov-10 2:45 
GeneralMy vote of 5 Pin
Tanvir Hasan Turan19-Sep-10 23:26
Tanvir Hasan Turan19-Sep-10 23:26 
GeneralMy vote of 5 Pin
AllenR24-Aug-10 23:17
professionalAllenR24-Aug-10 23:17 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman28-Aug-10 21:41
professionalMd. Marufuzzaman28-Aug-10 21:41 
GeneralMy vote of 5 Pin
Akhteruzzaman23-Aug-10 4:25
Akhteruzzaman23-Aug-10 4:25 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman23-Aug-10 22:24
professionalMd. Marufuzzaman23-Aug-10 22:24 
GeneralVery nice Pin
Taslima Bagum23-Aug-10 2:51
Taslima Bagum23-Aug-10 2:51 
GeneralRe: Very nice Pin
Md. Marufuzzaman23-Aug-10 2:59
professionalMd. Marufuzzaman23-Aug-10 2:59 
GeneralThanks Pin
Murat Kazanova23-Aug-10 1:56
Murat Kazanova23-Aug-10 1:56 
GeneralRe: Thanks Pin
Md. Marufuzzaman23-Aug-10 2:57
professionalMd. Marufuzzaman23-Aug-10 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.