Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 34 ยท Objects and the Pipeline

Objects, Properties and Methods

Learn how PowerShell objects work: .NET types, reading properties with dot notation, calling methods, static members, Get-Member and Select-Object -ExpandProperty.

Everything a cmdlet returns is an object. A running program, a file, a date, a service and even a piece of text are all objects, each built from a .NET type. An object bundles two things: properties, which hold data (Name, Length, Year), and methods, which perform actions (ToUpper(), AddDays(5)). Once this clicks, most of PowerShell stops being mysterious: sorting, filtering and formatting are all just operations on properties.

This lesson shows how to find an object's type, read properties with dot notation, call methods, use static members such as [Math]::Sqrt(), and pull a single value out of an object. The PowerShell code is read-only here; two JavaScript stand-ins model objects with members so you can see the ideas run. It builds on Getting Help, where you met Get-Member.

What an object is

When you run Get-Process, PowerShell does not print text from the program. It creates one System.Diagnostics.Process object per running process, then formats a few of their properties as a table for your eyes. All of the other properties are still there:

POWERSHELL
$p = Get-Process -Id $PID      # $PID is the id of this PowerShell session
$p.Name                        # a property: the process name
$p.Id
$p.StartTime
$p.WorkingSet64                # memory in bytes, a number, not text

The pattern is object.Property. The dot reaches inside the object. Because WorkingSet64 is a real number, you can compare and sort it without parsing anything.

Anatomy of an object

Type Which kind of thing it is.
Properties Named values it holds.
Methods Actions it can perform.

Get-Member shows all three.

1 / 2

Finding the type and its members

The object's type decides what members it has. Ask for the type with GetType(), or see it at the top of Get-Member output:

POWERSHELL
(Get-Date).GetType().FullName      # System.DateTime
"hello".GetType().FullName         # System.String
(42).GetType().Name                # Int32
@(1, 2, 3).GetType().Name          # Object[]

Get-Member lists the members:

POWERSHELL
Get-Date | Get-Member
Get-Date | Get-Member -MemberType Property
Get-Date | Get-Member -Name Add*

Each row shows the member name, its kind and its definition, such as a property's type or a method's parameters. Most members come from the .NET type, but PowerShell can add extras such as alias and script properties; Get-Member shows all of them together.

Use Format-List * (alias fl *) to look at the values of all properties of a single object:

POWERSHELL
Get-Date | Format-List *

Reading properties

Use a dot and the property name. Chain dots to go deeper:

POWERSHELL
$now = Get-Date
$now.Year
$now.DayOfWeek
$now.Date.Year                 # a property of a property

$name = "PowerShell"
$name.Length                   # 10

Remember that names are not case sensitive, so $name.length works. To read a property whose name is held in a variable or contains spaces, put it in quotes or use a variable: $obj.'Property Name' or $obj.$propName.

Properties of a list are special. With arrays, PowerShell 3 and later applies member enumeration: asking an array for a property returns that property from every element.

POWERSHELL
(Get-Process).Name             # every process name, as an array
(Get-ChildItem).Length         # sizes of the files (folders have none)

Calling methods

A method needs parentheses, even when it takes no arguments:

POWERSHELL
$text = "  powershell  "
$text.Trim()                   # "powershell"
$text.ToUpper()                # "  POWERSHELL  "
$text.Replace("shell", "Shell")
$text.Substring(2, 4)          # "powe"

$today = Get-Date
$today.AddDays(30)             # a new DateTime
$today.ToString("yyyy-MM-dd")

Two details trip up beginners. Without parentheses a method name prints the method's definition instead of running it ($text.Trim shows signatures). And methods that appear to modify an object, like AddDays and Replace, actually return a new value; the original is unchanged, because strings and dates are immutable.

Pass several arguments separated by commas: $text.Substring(2, 4). Do not use spaces instead of commas, as you would with cmdlet parameters.

Static members

Some members belong to a type instead of an individual object. You reach them by writing the type in square brackets, then :::

POWERSHELL
[Math]::Sqrt(144)              # 12
[Math]::Round(3.14159, 2)      # 3.14
[DateTime]::Now
[DateTime]::DaysInMonth(2024, 2)   # 29
[System.IO.Path]::GetExtension("report.pdf")   # .pdf
[Environment]::MachineName

Get-Member -Static lists them: [Math] | Get-Member -Static.

Extracting a value

A command that returns objects gives you objects even when you select one property. To get just the raw value, you have two options:

POWERSHELL
Get-Process -Id $PID | Select-Object Name          # an object with a Name property
Get-Process -Id $PID | Select-Object -ExpandProperty Name   # just the string
(Get-Process -Id $PID).Name                        # also just the string

The first form returns a new small object that has a single Name property. That is what you want to keep a table-like result. The second and third return the string itself, which is what you want when another command needs plain text.

Try it: an object and its members

The JavaScript object below stands in for a PowerShell object. It has properties, a method and a "type name". The script lists the members the way Get-Member would, then reads a property and calls a method.

JS Stand-in: reading properties and calling methods

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try it: static versus instance members

In PowerShell, instance members work on one object and static members belong to the type. JavaScript has the same split, so this stand-in uses Math (static) and a string (instance) side by side.

JS Stand-in: static members versus instance members

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real: the PowerShell blocks, which you can run in a PowerShell session. What is the stand-in: JavaScript has no Get-Member or .NET types, so the code only demonstrates the same split between data (properties) and actions (methods), and between instance and static members.

Common mistakes

  • Forgetting parentheses on a method call, so you print its signature.
  • Using spaces between method arguments instead of commas.
  • Expecting a method like Trim() to change the original variable. Assign the result: $text = $text.Trim().
  • Assuming that what you see in the table is everything. Use Get-Member or Format-List *.
  • Treating a number as text. Properties such as Length are numbers, so comparison works as you expect.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does (Get-Date).AddDays(7) return?

    Get-Date returns a DateTime object. AddDays is a method that returns a new DateTime; DateTime objects are immutable and the clock is not touched.
  2. 2. How do you call the static method that returns the square root of 144?

    Static members are reached with the type name in brackets, then ::, then the member.
  3. 3. Which command lists only the properties of the objects Get-Process returns?

    Get-Member takes -MemberType to filter to properties, methods and other kinds.
  4. 4. What is the difference between Select-Object Name and Select-Object -ExpandProperty Name?

    Without -ExpandProperty you keep a (smaller) object. With it you get the property's value itself, such as a plain string.

Summary

  • Every cmdlet returns .NET objects; a table is just a view of some of their properties.
  • Read properties with $object.Property; call methods with $object.Method(args) and always use parentheses.
  • GetType() and Get-Member show the type and its members; Format-List * shows all values.
  • Static members use [Type]::Member, for example [Math]::Sqrt(144).
  • Use Select-Object -ExpandProperty or a dotted access to get a raw value.

Next: The Pipeline.