One feature that gives PowerShell its incredible reach into both system administration and application development is its ability 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.
An example of such a class 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.”
Classes contain methods (which allow you to perform operations) and properties (which allow you to access information.)
The terms type and class can be used interchangeably.
[ClassName]::MethodName(parameter list)
Some methods relate only to the concept the class represents. For example, retrieving all running processes on a system relates to the general concept of processes. Methods that apply to the class/type as a whole are called static methods.
For example:
gets the process with the ID of 0 and displays the following output:
$objectReference.MethodName(parameter list)
Some methods relate only to specific, tangible realizations
(called instances) of a class. An example of this
would be 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 GetProcessById()
, for
example,), you may want 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:
Stores the process with ID of 0 into the $process
variable. It then calls the
Refresh()
instance method on that
specific process.
or:
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:
Although relatively rare, some types allow you to set the value of some static properties.
or:
Like instance methods, some properties relate only to
specific, tangible realizations (called
instances) of a class. An example of this would
be 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
[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:
Stores the current date into the $today
variable. It then calls the
DayOfWeek
instance property on
that specific date.
The two primary avenues for learning about classes and types are
the Get-Member
cmdlet and the
documentation for the .NET Framework.
To learn what methods and properties a given type supports, pass
it through the Get-Member
cmdlet.
Working with the Get-Member cmdlet | |
---|---|
Action | Result |
| All of the static methods and properties of a given type. |
| All of the static methods and
properties provided by the type in |
| All of the instance methods and
properties provided by the type in |
| All of the instance methods and properties provided by this specific instance of a class called Type. |
Another source of information about the classes in the .NET Framework is the documentation itself, available through the search facilities at http://msdn.microsoft.com.
Typical documentation for a class first starts with a general overview, then provides a hyperlink to the members of the class—the list of methods and properties it supports.
To get to the documentation for the members quickly, search for them more explicitly by adding the term “members” to your MSDN search term:
The documentation for the members of a class lists their methods and properties, and uses an S icon to represent the static methods and properties. Click the member name for more information about that member—including the type of object that the member produces.
When you specify a type name, PowerShell allows you to use a short form for some of the most common types.
PowerShell type shortcuts | |
---|---|
Type shortcut | Full classname |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$objectReference = New-Object TypeName [parameter list]
Although static methods and properties of a class generate
objects, you will often want to create them explicitly yourself.
PowerShell’s New-Object
cmdlet lets
you create an instance of the type you specify. The parameter list
must match the list of parameters accepted by one of the type’s
constructors, as documented on MSDN.
For example:
Most common types are available by default. However, many are available only once you load the library (called the assembly) that defines them. The MSDN documentation for a class includes the assembly that defines it.
To load an assembly, use the methods provided by the System.Reflection.Assembly
class:
PS >[Reflection.Assembly]::LoadWithPartialName("System.Web") GAC Version Location --- ------- -------- True v2.0.50727 C:WINDOWSassemblyGAC_32(…)System.Web.dll PS >[Web.HttpUtility]::UrlEncode("http://search.msn.com") http%3a%2f%2fsearch.msn.com
The LoadWithPartialName
method is unsuitable for scripts that you wish to share with others,
or use in a production environment. It loads the most current
version of the assembly, which may not be the same as the version
you used to develop your script. To load an assembly in the safest
way possible, use its fully qualified name with the [Reflection.Assembly]::Load()
method.
PowerShell allows you to access methods and properties on COM
objects the same way you would interact with objects from the .NET
Framework. To interact with a COM object, use its ProgId with the
–ComObject
parameter (often
shortened to –Com) on New-Object
:
PS >$shell = new-object -Com Shell.Application
PS >$shell.Windows() | select LocationName,LocationUrl
PowerShell supports two ways to add your own methods and
properties to any type: the Add-Member
cmdlet, and a custom types
extension file.
The Add-Member
cmdlet
allows you to dynamically add methods, properties, and more to an
object. It supports the following extensions:
Selected member types supported by the Add-Member cmdlet | |
---|---|
Member Type | Meaning |
| A property defined to alias another property: |
| A property defined by a: This method must be public, static, return results (nonvoid,) and take one parameter of type PsObject. |
| A property defined by the initial value you provide: |
| A property defined by the script
block you provide. In that script block, |
| A property defined as a shortcut
to a set of properties. Used in cmdlets such as |
| A method defined by a: This method must be
public, static, and take one parameter of type |
| A method defined by the script
block you provide. In that script block, |
While the Add-Member
cmdlet
allows you to customize individual objects, PowerShell also supports
configuration files that allow you to customize all objects of a
given type. For example, you might wish to add a Reverse()
method to all strings or a
HelpUrl
property (based on the
MSDN “Url Aliases”) to all types.
PowerShell adds several type extensions to the file
types.ps1xml, in the PowerShell installation
directory. This file is useful as a source of examples, but you
should not modify it directly. Instead, create a new one and use the
Update-TypeData cmdlet
to load
your customizations. The following command loads
Types.custom.ps1xml from the same directory as
your profile:
$typesFile = join-path (split-path $profile) “Types.Custom.Ps1Xml”Update-TypeData –PrependPath $typesFile
3.22.100.180