fbpx Work with .NET Objects | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Work with .NET Objects

Problem

You want to use and interact with one of the features that make PowerShell so powerful—its intrinsic support for .NET objects.

Solution

PowerShell offers ways to access methods (both static and instance) and properties.

To call a static method on a class, place the type name in square brackets, and then separate the class name from the method name with two colons:

[ClassName]::MethodName(parameter list)

To call a method on an object, place a dot between the variable that represents that object and the method name:

$objectReference.MethodName(parameter list)

To access a static property on a class, place the type name in square brackets, and then separate the class name from the property name with two colons:

[ClassName]::PropertyName

To access a property on an object, place a dot between the variable that represents that object and the property name:

$objectReference.PropertyName

Discussion

One feature that gives PowerShell its incredible reach into both system administration and application development is its capability to leverage Microsoft’s enormous and broad .NET Framework. The .NET Framework is a large collection of classes. Each class embodies a specific concept and groups closely related functionality and information. Working with the .NET Framework is one aspect of PowerShell that introduces a revolution to the world of management shells.

An example of a class from the .NET Framework is System.Diagnostics.Process— the grouping of functionality that “provides access to local and remote processes, and enables you to start and stop local system processes.”

The terms type and class are often used interchangeably.

Classes contain methods (which allow you to perform operations) and properties (which allow you to access information).

For example, the GetProcess cmdlet generates System.Diagnostics.Process objects, not a plaintext report like traditional shells. Managing these processes becomes incredibly easy, as they contain a rich mix of information (properties) and operations (methods). You no longer have to parse a stream of text for the ID of a process—you can just ask the object directly!

PS >$process = GetProcess Notepad PS >$process.Id 3872

Static methods

[ClassName]::MethodName(parameter list)

Some methods apply only to the concept the class represents. For example, retrieving all running processes on a system relates to the general concept of processes, instead of a specific process. Methods that apply to the class/type as a whole are called static methods.

For example:

PS >[System.Diagnostics.Process]::GetProcessById(0) This specific task is better handled by the GetProcess cmdlet, but it demonstrates PowerShell’s capability to call methods on .NET classes. It calls the static GetProcessById method on the System.Diagnostics.Process class to get the process with the ID of 0. This generates the following output:

Handles
NPM(K)
PM(K)
WS(K) VM(M)
CPU(s)
Id ProcessName

0
0
0
16
0

0 Idle

Instance methods

$objectReference.MethodName(parameter list)

Some methods relate only to specific, tangible realizations (called instances) of a class. An example of this would be stopping a process actually running on the system, as opposed to the general concept of processes. If $objectReference refers to a specific System.Diagnostics.Process (as output by the GetProcess cmdlet, for example), you may call methods to start it, stop it, or wait for it to exit. Methods that act on instances of a class are called instance methods.

The term object is often used interchangeably with the term instance.

For example:

PS >$process = GetProcess Notepad

PS >$process.WaitForExit() Stores the process with an ID of 0 into the $process variable. It then calls the WaitForExit() instance method on that specific process to pause PowerShell until the process exits.

To learn about the different sets of parameters (overloads) that a given method supports, type that method name without any parameters:

PS >$now = GetDate

PS >$now.AddDays

MemberType : Method OverloadDefinitions : {System.DateTime AddDays(Double value)}

TypeNameOfValue
: System.Management.Automation.PSMethod

Value
: System.DateTime AddDays(Double value)

Name
: AddDays

IsInstance
: True

Static properties

[ClassName]::PropertyName

or

[ClassName]::PropertyName = value

Like static methods, some properties relate only to information about the concept that the class represents. For example, the System.DateTime class “represents an instant in time, typically expressed as a date and time of day.It provides a Now static property that returns the current time:

PS >[System.DateTime]::Now

Saturday, June 2, 2007 4:57:20 PM This specific task is better handled by the GetDate cmdlet, but it demonstrates PowerShell’s capability to access properties on .NET objects.

Although relatively rare, some types allow you to set the value of some static properties as well: for example, the [System.Environment]::CurrentDirectory property. This property represents the process’s current directory—which represents PowerShell’s startup directory, as opposed to the path you see in your prompt.

Instance properties

$objectReference.PropertyName

or

$objectReference.PropertyName = value

Like instance methods, some properties relate only to specific, tangible realizations (called instances) of a class. An example of this would be the day of an actual instant in time, as opposed to the general concept of dates and times. If $objectReference refers to a specific System.DateTime (as output by the GetDate cmdlet or [System. DateTime]::Now, for example), you may want to retrieve its day of week, day, or month. Properties that return information about instances of a class are called instance properties.

For example:

PS >$today = GetDate PS >$today.DayOfWeek Saturday

This example stores the current date in the $today variable. It then calls the DayOfWeek instance property to retrieve the day of the week for that specific date.

With this knowledge, the next questions are: “How do I learn about the functionality available in the .NET Framework?” and “How do I learn what an object does?”

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say