liveBook · Manning

Object type

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

An object type is the runtime classification that determines an object's available structure and behavior.

An object type classifies the data held by a PowerShell object and determines which members and operations are available. Check a value with Get-Member (gm) before deciding what a later pipeline command can do. section 8.9

Use explicit types when a variable must hold predictable data:

  • [int] stores integer numbers; [single] and [double] store decimal numbers; [string] stores characters; [char] stores one character; and [xml] validates XML markup. section 16.6
  • Declare [int]$number before reading input to convert valid input into an integer, so multiplication produces numeric results instead of string repetition. section 16.6
  • PowerShell rejects input that cannot be converted and continues enforcing the declared type until the variable is explicitly retyped. section 16.6

Inspect and constrain a variable

Read input as an integer, then verify its type and value: [int]$number = Read-Host "Enter a number"; $number | Get-Member; $number = $number * 10; $number. Entering 100 reports System.Int32 and produces 1000. Entering Hello raises a conversion error.

PS /Users/jamesp/> Get-Process; Get-History
 
 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
...
      0     0.00       1.74       0.25    1244   1 UsageTrackingAg
      0     0.00       0.68       0.19     710   1 USBAgent
      0     0.00       4.12       6.37     544   1 useractivityd
      0     0.00       5.44       8.00     407   1 UserEventAgent
      0     0.00       7.50       3.43     428   1 usernoted
      0     0.00       3.44       8.71     506   1 ViewBridgeAuxil
      0     0.00       5.91       0.08   27019 ...19 VTDecoderXPCSer
      0     0.00       5.92       0.07   89048 ...48 VTDecoderXPCSer
      0     0.00      10.79      50.02     434   1 WiFiAgent
      0     0.00       1.11       0.20    1242   1 WiFiVelocityAge
      0     0.00      10.28       4.30   20921 ...21 XprotectService
 
Id                 : 1
CommandLine        : Update-TypeData -Path /tmp/Uri.Types.ps1xml
ExecutionStatus    : Completed
StartExecutionTime : 9/21/2019 12:20:03 PM
EndExecutionTime   : 9/21/2019 12:20:03 PM
Duration           : 00:00:00.0688690
 
Id                 : 2
CommandLine        : Update-TypeData -Path /tmp/Uri.Types.ps1xml
ExecutionStatus    : Completed
StartExecutionTime : 9/21/2019 12:21:07 PM
EndExecutionTime   : 9/21/2019 12:21:07 PM
Duration           : 00:00:00.0125330eyp

Compare the formatting result when process objects and history objects share a pipeline: the display begins as a process table and then falls back to a list. section 11.9.2

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains type inspection, common declarations, conversion, enforcement, and mixed-pipeline formatting, while deferring custom combined output.
Examples
Demonstrates integer input, Get-Member inspection, invalid conversion, variable retyping, and mixed process-history formatting.
What it assumes
Assumes basic PowerShell commands, variables, pipelines, and Get-Member while demonstrating type behavior through runnable examples.
What you can do after
Diagnose pipeline behavior with Get-Member and apply explicit variable types to obtain predictable arithmetic and conversion errors.
How it connects
Connects object types with PowerShell pipelines, variables, Get-Member, formatting, and type conversion.

Also explained in

Compare how 2 books explain object type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.