Powershell Day 11

Day 11 COM and WMI

Powershell is a very powerful language, with Powershell we can achieve same result with the several ways. Microsoft programming language uses several objects COM (Component

Object Model), WMI (Windows Management Instrumentation) and .Net for programming, using Powershell we can create these object as well.

While development Powershell has several cmdlets but it is not easy to make this cmdlets for each and everything, so Powershell can use an existing objects for COM, WMI and .Net objects. To make Powershell more strong.  There is command called “New-object” by which we can create an object for any time. And use that object, which has its own methods and property. And inturns that objects will be of windows objects –COM/ .Net. so the compatibility wise it will be efficient.

As I am not a Windows programmer so I may not be the right person to go detailed into it but would like to give an introduction on it.

.Net : As I stated ealier blogs that Powershell is uses .Net Framework. To use the . Net objects we have to first load its assembly as default its not loaded into Powershell. Once we load the assembly we can create an object of and manipulate that object including GUI objects.





$form = New-Object Windows.Forms.Form

$form.Text = “My First Form”

$button = New-Object Windows.Forms.Button

$button.text=”Push Me!”





This example will create a simple form and display dialogbox with ok button, once we press ok the dialog box will close.

COM : objects are objects like applications eg. Xls, word, internet explorer, so using COM object we can create an excel file and manipulate it.  


New-Object [-ComObject ] <String > -strict

For just to understand it we will familier with the terms in excels

Excel is having an object which contains “WorkBook”, workbook contains “WorkSheets”- sheets, and every sheet contains cells (combination of rows and columns), so everything will be on workbook

Following is the steps for COM objects

  1. Create a new comobject

$xl=new-object –comobject excel.application

##this will create a new excel application com object

  1. Add a new workbook object for that excel com object


  1. Now on that workbook make an worksheet-


Now you are ready with excel worksheet. So now on you are good to work with cells onf worksheet and edit that.

  1. Work on worksheet cells




  1. This all this are working background, so if you would like to see the xls file. Make it visible.


If you want to disable the xls file


  1. Now For saving this xls workbook.


  1. To quit the excel file.


Btw if you want to see the properties and methods belong to the object $xl you can use |get-member (gm) – cmdlet –which you can use for any object to view their members

$xl |gm

Like this you can work on any application

internet Explorer



Shell is a very powerful object creation for COM object. There are two ways to work with shell.

  1. Create a shell application object.

$Shell =new-object –COM shell.application

Using this object we can work on the COM application objects, eg. Window explorer, Control panel. Etc.

Eg. $Shell.explore(“C:\”)

See more members using $shell |gm

Shell also support WScript.shell as it’s a more easy to work on the objects as

Create an object for WSScript.shell

$WShell=new-object –COM WScript.shell


if ($wshell.AppActivate(“Calculator”))



start-sleep 10



This script will use the wshell and run the calc.exe which will open the calculator now if you want to work on calc using Powershell. Activate it and send the keys using sendkeys method. At the end if you want to close – send Alt+F4 to calc to close.

Isn’t it cool?.

WMI: Another way to work on windows is using WMI. Here things are little different. We have get-wmiobject a separate cmdlet to work on it. 

WMI is windows management used only for C++, VbScript in that also it was very difficult to code. On the other hand WMI is very simple and powerful in Powershell.


[-Namespace <String >]

[-ComputerName <String []>]

[-Credential <PSCredential >]

[-List ]

To see the list of options in Get-WMIObjects

Get-WmiObject –list

Very long list right… okay you can filter it. Like to see only win32 related objects

Get-WmiObject -list | where {$_ -match “win32_*”}

Looks good now. Right okay here are some of the common and important win32 wmi options.

#to get the domain information

$d=get-wmiobject win32_computersystem -property domain |select domain

#infomation about operating system

PS C:\> get-wmiobject win32_OperatingSystem

#list of hotfixes

PS C:\> get-wmiobject win32_quickfixengineering

#Bios information

PS C:\> get-wmiobject win32_Bios

#logical drives

PS C:\>  get-wmiobject win32_Logicaldisk

There are some other win32 options like

PS C:\>  get-wmiobject win32_Process

PS C:\>  get-wmiobject win32_Service

More information about that wmi

PS C:\>  get-wmiobject win32_Service | FL * |more

FL * means you want all the information about the columns.

As I said we can get the same result with several ways so here it is we can use the get-process or get-wmiobject win32_Process.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.