Geek School: PowerShell Objects - A Beginner's Guide

The Significance of Objects in PowerShell
A core principle for mastering PowerShell is grasping the concept of objects. This exploration will detail how objects elevate PowerShell's capabilities, distinguishing it from conventional shells currently available.
Review of Previous Articles
Before proceeding, it is recommended to review the preceding articles in this series to build a solid foundation.
- Learn How to Automate Windows with PowerShell: This article provides an introduction to automating tasks using PowerShell.
- Learning to Use Cmdlets in PowerShell: This piece focuses on understanding and utilizing cmdlets, the fundamental commands in PowerShell.
Further installments of this series will be published throughout the week, offering continued insights into PowerShell's functionalities.
PowerShell’s object-oriented nature allows for more efficient and predictable scripting. Instead of dealing with text, you work with structured data.
This approach facilitates easier manipulation and filtering of information. The shell passes entire objects, not just strings of text.
Consequently, PowerShell scripts become more robust and less prone to errors. The use of objects ensures data integrity throughout the process.
Objects
Have you considered the fundamental difference between PowerShell and conventional shells such as Bash or the older command prompt? The distinction is straightforward: traditional shells deliver text-based output, complicating tasks like formatting and filtering. While utilities like sed and grep exist to assist, substantial text parsing often necessitates a strong grasp of regular expressions.
PowerShell, leveraging the .Net framework, adopts a distinct methodology, employing objects rather than plain text. Objects serve as representations of entities, comprising both attributes and the actions applicable to them. Consider, for example, the components of a bicycle and their respective uses.
.Net objects function similarly, with two key distinctions. The "Parts" are referred to as properties, and the "Instructions" are known as methods.
To illustrate, let's represent a Windows Service as an object. We might define it using properties like Service Name, State, and Description. Furthermore, interaction with the service requires methods such as Start, Stop, and Pause.
The properties and methods of an object can be examined by utilizing the Get-Member cmdlet. PowerShell cmdlets typically output objects based on underlying .Net framework types.
However, custom objects can be created using languages like C# or by utilizing the PSObject type when necessary.
Understanding Properties and Methods
Properties define the characteristics of an object. They hold data that describes the object's state. Think of them as adjectives describing a noun.
Methods, conversely, represent the actions an object can perform. They are the verbs associated with the object, dictating what it can do.
How to Explore Objects in PowerShell
PowerShell provides a simple way to inspect the properties and methods of any object. Simply pipe the object to the Get-Member cmdlet.
This cmdlet will display a list of all available properties and methods, allowing you to understand the object's capabilities and data structure.
Benefits of Using Objects
- Structured Data: Objects provide a structured way to represent information, making it easier to manipulate and analyze.
- Simplified Parsing: No need for complex text parsing or regular expressions.
- Enhanced Functionality: Methods allow you to perform actions on objects directly.
Understanding PowerShell Pipelines
Many Linux shells utilize pipelines, enabling the transmission of a command’s text output as input to the subsequent command. PowerShell expands upon this concept by facilitating the passage of objects—the output of one cmdlet—as input to the following cmdlet within the pipeline. A key aspect of this functionality is identifying the object type returned by a cmdlet, a process easily accomplished using the Get-Member cmdlet.
Inspecting Objects with Get-Member
Consider the following example:
Get-Service | Get-Member
This command sequence directs the output of Get-Service to Get-Member for analysis.

Within PowerShell, both properties and methods are collectively referred to as class members. Consequently, Get-Member is employed to retrieve a comprehensive list of an object’s methods and properties. Crucially, this cmdlet also reveals the underlying object type.
As demonstrated in the screenshot, Get-Service returns objects of the type:
System.ServiceProcess.ServiceController
Pipeline Compatibility and Cmdlet Discovery
PowerShell operates with objects, not simply text. Therefore, not all cmdlets are compatible for chaining together via the pipeline[1]. It becomes necessary to identify cmdlets designed to accept objects of a specific type, such as System.ServiceProcess.ServiceController.
Get-Command -ParameterType System.ServiceProcess.ServiceController

The output reveals the presence of Stop-Service, a cmdlet that appears relevant. Further investigation of its help documentation is warranted.
Exploring Cmdlet Help
To understand Stop-Service’s capabilities, use the following command:
Get-Help –Name Stop-Service

The help information indicates that the InputObject parameter accepts an array of ServiceController objects. Parameters named InputObject often accept pipeline input, but a thorough examination of the parameter’s full help documentation is prudent.
Confirming Pipeline Input
To verify pipeline input acceptance, execute:
Get-Help -Name Stop-Service –Full

This confirms that the InputObject parameter indeed accepts input from the pipeline.
Putting it All Together
Based on this analysis, we can confidently state:
- Get-Service returns ServiceController objects.
- Stop-Service’s InputObject parameter accepts one or more ServiceController objects.
- The InputObject parameter is capable of receiving pipeline input.
This allows us to construct the following command:
Get-Service -Name 'Apple Mobile Device' | Stop-Service

This concludes our exploration for now. In a future discussion, we will delve into techniques for formatting, filtering, and comparing objects within the PowerShell pipeline.
Assignment: Object Pipeline Study
This assignment requires focused review of the Object Pipeline concept.
Preparation
Thoroughly research and familiarize yourself with the intricacies of the Object Pipeline.
Understanding this pipeline is crucial for subsequent tasks.
Should any uncertainties arise during your study, several avenues for clarification are available.
Contact & Support
- You can reach out via Twitter to @taybgibb with any questions.
- Alternatively, feel free to post your questions directly as a comment.
Prompt assistance will be provided to ensure complete comprehension.
Effective learning is supported through open communication and collaborative problem-solving.