The get-member cmdlet shows what properties and events are available for a command. The following listing shows a partial output of the get-date | get-member command:
PS C:\ >get-date | get-member
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method System.DateTime Add(System.TimeSpan value)
AddDays Method System.DateTime AddDays(double value)
AddHours Method System.DateTime AddHours(double value)
. . .
ToShortDateString Method string ToShortDateString()
ToShortTimeString Method string ToShortTimeString()
. . .
Date Property System.DateTime Date {get;}
Day Property System.Int32 Day {get;}
DayOfWeek Property System.DayOfWeek DayOfWeek {get;}
DayOfYear Property System.Int32 DayOfYear {get;}
Hour Property System.Int32 Hour {get;}
Kind Property System.DateTimeKind Kind {get;}
Millisecond Property System.Int32 Millisecond {get;}
Minute Property System.Int32 Minute {get;}
Month Property System.Int32 Month {get;}
Second Property System.Int32 Second {get;}
Ticks Property System.Int64 Ticks {get;}
TimeOfDay Property System.TimeSpan TimeOfDay {get;}
Year Property System.Int32 Year {get;}
The following table shows how you can use this information retrieved with the get-member cmdlet by accessing properties or executing methods.
Note
Most properties and methods can be accessed using the dot operator, but the command must be in parentheses. The format is (command).property. As you can see, the dot operator is a period after the command. The following table includes some dot operator examples.
Exploring get-date | Comments |
---|
Get available members.
PS C:\> get-date | get-member
PS C:\> get-date | get-member -force
| This shows all of the members of the get-date cmdlet. The MemberType column identifies the members as methods and properties.
The Definition column identifies how the member can be used, such as {get;} to indicate a property can be retrieved, {set;} to indicate a property can be configured, or both.
The -force shows all members including intrinsic members. |
Format member output to view definitions.
PS C:\> get-date | get-member | format- list
| Displayed
data often won’t fit on the screen and instead ends with an ellipsis
(...). which usually simply means “there’s more.” You can use the format-list command to view all the data in a list format instead of a table format. |
Retrieve a property using the dot operator.
(command). property PS C:\> (get-date).hour 19
| Most properties can be retrieved simply by enclosing the cmdlet in parentheses and using a dot and the property name.
The example shows the hour as 19 (or 7 PM) on a 24-hour clock.
Tip
This works when there is only one instance of a class. There is only
one current date and time, so this works. It wouldn’t work for get-service because there are multiple instances of services running. The next table shows how to handle objects with multiple instances.
|
Execute a method with the dot operator.
(command).method() PS C:\> (get-date). toshorttimestring() 9:13 AM
| Methods
are executed with parentheses () at the end. Some methods accept (and
require) parameters that can be included in the parentheses, but an
empty parameter list often works.
Note
The get-member output shows that the toshorttimestring method has an empty parameter list in the description.
The example shows the output of the toshorttimestring method. |
Execute a method with a parameter.
command. method( parameter) PS C:\> (get-date). addhours(5). toshorttimestring() 2:13 PM
| The addhours() method requires a parameter and adds the provided number to the current hours.
Note
The get-member output shows that the addhours method requires a parameter of type double. Double is a number.
Notice that you can sometimes use more than one method by
separating them with dots. The example adds five hours to the current
time of 9:13 AM and then converts it to a short time string (such as
2:13 PM). |
There is a difference in syntax when there’s just a
single instance of an object and when there are multiple instances of
an object. For example, the get-date
cmdlet returns today’s date, and there is only one value of today’s
date. However, there are always multiple services identified when you
execute the get-service cmdlet.
Different procedures are required when you have multiple instances. The following table shows some examples.
Exploring get-service | Comments |
---|
Get available members.
PS C:\> get-service | get-member
| This shows all of the members of the get-service cmdlet. The MemberType column identifies the members as methods and properties.
The Definition column identifies how the member can be used, such as {get;} to indicate a property can be retrieved. |
Retrieve a property.
(command-name name).property (get-service -name servicename).status PS C:\ > (get-service -name msftpsvc).status Running
| Properties of specific services can be retrieved using a unique identifier such as the name.
Note
The (get-service).status command returns a null value because it doesn’t know which service to query.
You can use the get-service command to identify the names of services to use with the -name switch.
The example checks the status of the FTP Publishing Service (msftpsvc). It returns running, stopped, or paused. |
Execute a method.
(command -name name).method() PS C:\> (get-service -name msftpsvc).stop() PS C:\> (get-service -name msftpsvc).start()
| Methods
are executed with parentheses () at the end. Some methods accept
parameters that can be included in the parentheses, but an empty
parameter list often works.
Note
The output of get-service | get-member st* shows that the stop and start methods have empty parameter lists, as listed in the definition as Start() and Stop().
These examples show how to stop and start the FTP Publishing Service. |