liveBook · Manning

Method

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

A method is a callable operation associated with an object that can direct the object to perform an action.

A method is a callable operation associated with one object. It tells that object to perform an action, such as terminating a process, changing a string, or calculating a date. Properties provide information; methods perform operations; both are members of an object. section 8.1 section 26.3

Use these practical rules when working with methods:

  • Discover methods by piping an object to Get-Member, or use its alias gm: Get-Process | gm. section 8.3
  • Call a method after a period and always add parentheses, even when it takes no parameters; put required arguments inside the parentheses. section 15.5.4
  • Use ForEach-Object when calling a method for each piped object: Get-ChildItem | ForEach-Object { $_.Delete() }. section 15.5.4 section 15.7
  • Prefer a cmdlet when one provides the same administrative action; Stop-Process is usually simpler than directly calling a process object's Kill method. section 8.5

Manipulating strings and dates

Strings and dates expose useful methods. `$computername.ToLower()` converts SERVER17 to server17, while `$username.Trim()` removes surrounding spaces. To calculate an earlier date, assign `Get-Date` to `$today`, then use `$today.AddDays(-90)`; use `.ToShortDateString()` for a short format. section 26.3 section 26.4

Get-Process | gm

Inspect a process object's type, properties, and available methods with Get-Process | gm before deciding how to act on it. section 8.3

Methods behave differently across remoting boundaries. Objects returned from remoting are generally deserialized, read-only copies without operational methods; put actions in the remote script block while objects remain live. section 13.7.1

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

How deep it goes
Explains discovery, invocation syntax, pipeline use, collection behavior, and remoting limitations, while deferring most direct .NET programming.
Examples
Demonstrates process termination, file deletion, string conversion and trimming, date arithmetic, and method discovery through Get-Member.
What it assumes
Assumes basic PowerShell commands, pipelines, variables, and objects, while introducing object members and direct method syntax.
What you can do after
Apply it via Get-Member and PowerShell syntax to discover methods, invoke them on objects, and choose simpler cmdlets when available.
How it connects
Connects methods with objects, properties, collections, Get-Member, cmdlets, pipelines, variables, remoting, strings, and dates.

Also explained in

Compare how 20 books explain method →

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.