Click here to Skip to main content
Click here to Skip to main content
Go to top

A Quick Guideline for Microsoft Windows PowerShell - Part 4

, 22 Aug 2010
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Md. Marufuzzaman
CEO
Bangladesh Bangladesh
A highly experienced leader with successful track record of software development, product innovations, brand management and corporate communication etc. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by codeproject.com and also selected as a mentor of codeproject.com. Published over 100 technical articles in various software development resource sites (i.e., codeprojetc.com, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.
 
Over ten years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, development, services, data management and analytics, customer experience management, content services, digital analytics and optimization.
 
An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint. Believes in transparency, commitment and teamwork.
 
Expertise: Software/Solution Architect, SaaS platform base application, Large scale win32/web based business software solutions, enterprise applications, integration, etc.
 
Technologies/Tools: Microsoft.Net version 05/08/10/12, Microsoft SQL Server version 7/2K/05/08/12 , Oracle version 10/11, MySql version 5.1, 5.5, PS2, Visual C#, R, VB.NET, ASP.NET, PHP, API, MVC, WebAPI , Add-In Visual Basic etc.,. I have also more than two years’ of strong experience in mobile-VAS (platform development).
 
Points of Interest: Technology and research & development especially focused on business functionalities and social business areas as well, few stuff including:
 
1.R&D on new techniques as required to increase business revenue.
2.Urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc., using innovative technical solutions.
3.Research and innovative product development.
4.Etc.
Follow on   Twitter   LinkedIn

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberlibenwei22-Nov-10 16:57 
GeneralRe: My vote of 5 PinmvpMd. Marufuzzaman5-Oct-11 23:17 
GeneralMy vote of 5 PinmemberMohsiul Haque2-Nov-10 2:45 
GeneralMy vote of 5 PinmemberTanvir Hasan Turan19-Sep-10 23:26 
GeneralMy vote of 5 PinmemberAllenR24-Aug-10 23:17 
GeneralRe: My vote of 5 PinmvpMd. Marufuzzaman28-Aug-10 21:41 
GeneralMy vote of 5 PinmemberAkhteruzzaman23-Aug-10 4:25 
GeneralRe: My vote of 5 PinmvpMd. Marufuzzaman23-Aug-10 22:24 
GeneralVery nice PinmemberTaslima Bagum23-Aug-10 2:51 
GeneralRe: Very nice PinmvpMd. Marufuzzaman23-Aug-10 2:59 
GeneralThanks PinmemberMember 451870623-Aug-10 1:56 
GeneralRe: Thanks PinmvpMd. Marufuzzaman23-Aug-10 2:57 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 23 Aug 2010
Article Copyright 2010 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid