TExcelTAD
Home › PowerShell › PowerShell Objects

PowerShell Objects

Everything in PowerShell is an object: a number, a string, a file, a date, the result of a web request. An object bundles properties (named values) and methods (actions you can call). Understanding this is the key to PowerShell, because it lets you skip the text parsing that other shells need.

Properties

Read a property with a dot. Properties can themselves be objects with more properties:

powershell · try it yourself
Set-Content ./report.txt 'Quarterly numbers'
$file = Get-Item ./report.txt

$file.Name
$file.Extension
$file.Length
$file.Directory.Name
report.txt
.txt
18
tmp

Get-Item returned a FileInfo object. $file.Directory is another object (a DirectoryInfo for /tmp), and we read its Name.

Methods

Call a method with a dot, the method name and parentheses, even when there are no arguments:

powershell · try it yourself
$city = 'Reykjavik'
$city.ToUpper()
$city.Length
$city.StartsWith('Rey')
$city.Replace('vik', 'VIK')
$city.Substring(0, 4)
REYKJAVIK
9
True
ReykjaVIK
Reyk

Without the parentheses PowerShell shows you information about the method (its overloads) instead of calling it, a handy way to check which arguments it takes.

Member enumeration

If you ask an array of objects for a property, PowerShell collects that property from every element. This is called member enumeration:

powershell · try it yourself
$planets = @(
    [pscustomobject]@{ Name = 'Mercury'; Moons = 0 }
    [pscustomobject]@{ Name = 'Earth';   Moons = 1 }
    [pscustomobject]@{ Name = 'Mars';    Moons = 2 }
)

$planets.Name
"Total moons: $(($planets.Moons | Measure-Object -Sum).Sum)"
$planets.Name.ToUpper()
Mercury
Earth
Mars
Total moons: 3
MERCURY
EARTH
MARS

$planets.Name is shorthand for $planets | ForEach-Object { $_.Name }. It even works for methods: .ToUpper() was called on each name.

Select-Object: shaping objects

Select-Object picks the properties you want and creates new, smaller objects. It can also add calculated properties with a hashtable containing a Name and an Expression:

powershell · try it yourself
$planets = @(
    [pscustomobject]@{ Name = 'Mercury'; Moons = 0; DistanceAU = 0.39 }
    [pscustomobject]@{ Name = 'Earth';   Moons = 1; DistanceAU = 1.0 }
    [pscustomobject]@{ Name = 'Mars';    Moons = 2; DistanceAU = 1.52 }
)

$planets | Select-Object Name, @{ Name = 'DistanceKm'; Expression = { $_.DistanceAU * 149.6e6 } }

Name      DistanceKm
----      ----------
Mercury  58344000.00
Earth   149600000.00
Mars    227392000.00

-ExpandProperty: the value, not an object

A frequent beginner trap: Select-Object Name returns objects that have a Name property, not the names themselves. Use -ExpandProperty to get the raw values:

powershell · try it yourself
$planets = @(
    [pscustomobject]@{ Name = 'Mercury' }
    [pscustomobject]@{ Name = 'Earth' }
)

$a = $planets | Select-Object Name
$b = $planets | Select-Object -ExpandProperty Name

$a[0].GetType().Name
$b[0].GetType().Name
"With Select-Object:    $($a[0])"
"With -ExpandProperty: $($b[0])"
PSCustomObject
String
With Select-Object:    @{Name=Mercury}
With -ExpandProperty: Mercury

Turned into a string, an object from Select-Object looks like @{Name=Mercury}, which is rarely what you want. The expanded value is just the text Mercury.

Default display

Objects don't "look" like anything until they reach the end of the pipeline. Then PowerShell's formatter picks a view: a table when an object has four or fewer properties, a list when it has more. You can override that with Format-Table and Format-List (see the Formatting Output lesson).

Tip: When you're unsure what you have, ask: $x.GetType().FullName for the type and $x | Get-Member for the members.

Test yourself

What is the difference between Select-Object Name and Select-Object -ExpandProperty Name?

  1. There is none
  2. The first returns objects with a Name property, the second returns the Name values themselves
  3. The second sorts by Name
  4. The first only works on files

Given an array $files of file objects, what does $files.Name return?

  1. An error: arrays have no Name
  2. The Name of the first file
  3. The Name of every file (member enumeration)
  4. The array's variable name

How do you call the ToLower method of $s?

  1. $s.ToLower
  2. $s.ToLower()
  3. ToLower($s)
  4. $s->ToLower()