Objects: .NET, WMI, and COM

PowerShell is an object-oriented product—all the language features and constructs are based on objects: cmdlets emit objects, variables are objects, and language constructs allow you to manipulate objects. You can use the properties of an object and can call methods an object contains. While some PowerShell users may not make much direct use of this object orientation, PowerShell is able to make use of .NET and WMI objects, both at the command line and in scripts. Like most of PowerShell's features, we could devote an entire book to the subject of objects in PowerShell.

PowerShell is built on the .NET Framework and enables you to access and use most of the classes within the framework. A restriction of PowerShell version 1 is that the asynchronous classes and methods are not supported.

Using .NET objects is pretty easy since much of it happens by default. You can either let PowerShell determine the .NET classes by default—for example, typing LS or DIR in a filesystem drive produces a set of file and directory objects. You can see this by piping the output of a command to the Get-Member cmdlet, as shown here:

PSH [D:\foo]:ls

    Directory: Microsoft.PowerShell.Core\FileSystem::D:\foo


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        14/10/2007     12:04            bar
-a---        09/09/2007     12:30          3 foo.txt
-a---        14/10/2007     19:42         13 helloworld.ps1
PSH [D:\foo]: dir | Get-Member TypeName: System.IO.DirectoryInfo Name MemberType Definition ---- ---------- ---------- ...

Get Windows Server 2008: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.