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:
Set-Content ./report.txt 'Quarterly numbers'
$file = Get-Item ./report.txt
$file.Name
$file.Extension
$file.Length
$file.Directory.Namereport.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:
$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:
$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:
$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:
$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().FullNamefor the type and$x | Get-Memberfor the members.
Test yourself
What is the difference between Select-Object Name and Select-Object -ExpandProperty Name?
- There is none
- The first returns objects with a Name property, the second returns the Name values themselves
- The second sorts by Name
- The first only works on files
Given an array $files of file objects, what does $files.Name return?
- An error: arrays have no Name
- The Name of the first file
- The Name of every file (member enumeration)
- The array's variable name
How do you call the ToLower method of $s?
- $s.ToLower
- $s.ToLower()
- ToLower($s)
- $s->ToLower()