Bird
Raised Fist0
PowerShellscripting~10 mins

Why PowerShell is object-oriented - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why PowerShell is object-oriented
PowerShell Command Runs
↓
Returns Objects, not Text
↓
Objects Have Properties & Methods
↓
You Can Access & Use Them Directly
↓
Enables Powerful Automation & Scripting
PowerShell commands return objects with properties and methods, letting you work with structured data directly, unlike plain text output.
Execution Sample
PowerShell
Get-Process | Select-Object -First 1 | Format-List -Property Name,Id
$proc = Get-Process | Select-Object -First 1
$proc.Name
$proc.Id
This code gets one process object, shows its Name and Id, then accesses those properties directly.
Execution Table
StepActionCommand/ExpressionResult/Output
1Get first process objectGet-Process | Select-Object -First 1Returns a Process object with many properties
2Display Name and Id propertiesGet-Process | Select-Object -First 1 | Format-List -Property Name,IdShows Name and Id as text output
3Assign process object to variable$proc = Get-Process | Select-Object -First 1Variable $proc holds the Process object
4Access Name property$proc.NameOutputs the process name as string
5Access Id property$proc.IdOutputs the process ID as number
💡 All steps complete; demonstrated object retrieval and property access
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5
$procnullProcess object (e.g. Name='powershell', Id=1234)Same object, Name property accessedSame object, Id property accessed
Key Moments - 3 Insights
Why does $proc.Name give a simple string instead of text output?
Because $proc is an object with a Name property; accessing $proc.Name returns that property's value directly, as shown in execution_table step 4.
How is PowerShell output different from traditional command-line text output?
PowerShell outputs objects with properties and methods, not just plain text lines. This is shown in step 1 where Get-Process returns an object, not text.
Can you use methods on the objects returned by PowerShell commands?
Yes, objects have methods you can call directly, enabling powerful scripting. This is implied by the object-oriented nature shown in the concept flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type of data is stored in $proc after step 3?
AA plain text string
BA Process object with properties
CA number representing process ID
DAn array of strings
💡 Hint
Check the 'Result/Output' column in step 3 of the execution_table
At which step does the script access the Name property of the process object?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look for '$proc.Name' in the 'Command/Expression' column of the execution_table
If PowerShell returned plain text instead of objects, what would happen to $proc.Name access?
AIt would cause an error because text has no properties
BIt would still work the same
CIt would return the whole text output
DIt would return null silently
💡 Hint
Refer to the key_moments section explaining object vs text output
Concept Snapshot
PowerShell commands return objects, not plain text.
Objects have properties and methods you can access directly.
This lets you work with structured data easily.
Use variables to hold objects and access their members.
This object-oriented design enables powerful automation.
Full Transcript
PowerShell is object-oriented because its commands return objects with properties and methods instead of plain text. For example, Get-Process returns a process object. You can assign this object to a variable like $proc and then access properties such as $proc.Name or $proc.Id directly. This is different from traditional shells that return text output only. Working with objects allows you to write more powerful and flexible scripts by manipulating structured data easily. The execution steps show getting a process object, displaying some properties, and accessing them through variables. This object-oriented approach is a core reason PowerShell is powerful for automation.

Practice

(1/5)
1. Why is PowerShell considered an object-oriented scripting language?
easy
A. Because it uses objects with properties and methods to handle data
B. Because it only works with text files
C. Because it requires classes to run scripts
D. Because it cannot handle variables

Solution

  1. Step 1: Understand what object-oriented means in PowerShell

    PowerShell treats data as objects that have properties (data) and methods (actions).
  2. Step 2: Identify why this matters

    This allows scripts to easily access and manipulate data in a structured way.
  3. Final Answer:

    Because it uses objects with properties and methods to handle data -> Option A
  4. Quick Check:

    Object-oriented = uses objects with properties and methods [OK]
Hint: Objects have properties and methods, not just text [OK]
Common Mistakes:
  • Thinking PowerShell only works with text
  • Believing classes are always required
  • Assuming variables cannot hold objects
2. Which of the following is the correct way to access the 'Length' property of a string object in PowerShell?
easy
A. $string.Length()
B. $string.Length
C. Length($string)
D. $string->Length

Solution

  1. Step 1: Recall property access syntax in PowerShell

    Properties are accessed using dot notation without parentheses.
  2. Step 2: Apply to 'Length' property

    Use $string.Length to get the length of the string.
  3. Final Answer:

    $string.Length -> Option B
  4. Quick Check:

    Properties use dot notation without () [OK]
Hint: Use dot and property name without parentheses [OK]
Common Mistakes:
  • Adding parentheses to properties
  • Using function-like syntax for properties
  • Using arrow notation which is not valid
3. What will be the output of this PowerShell code?
$date = Get-Date
$date.DayOfWeek
medium
A. The current day of the week as a string, like 'Monday'
B. An error because DayOfWeek is not a property
C. The current date in MM/DD/YYYY format
D. The time part of the date only

Solution

  1. Step 1: Understand Get-Date output

    Get-Date returns a DateTime object with many properties including DayOfWeek.
  2. Step 2: Access DayOfWeek property

    $date.DayOfWeek returns the day of the week as a string like 'Monday'.
  3. Final Answer:

    The current day of the week as a string, like 'Monday' -> Option A
  4. Quick Check:

    DayOfWeek property returns day name [OK]
Hint: Get-Date returns object; DayOfWeek is a property [OK]
Common Mistakes:
  • Expecting a date string instead of day name
  • Thinking DayOfWeek is a method needing ()
  • Assuming it returns time or error
4. Identify the error in this PowerShell script:
$file = Get-Item 'C:\temp\file.txt'
$size = $file.Length()
Write-Output $size
medium
A. File path must use forward slashes
B. Get-Item cannot get file information
C. Write-Output cannot print variables
D. Using parentheses after 'Length' property causes an error

Solution

  1. Step 1: Check property access syntax

    Properties like 'Length' are accessed without parentheses in PowerShell.
  2. Step 2: Identify the error

    $file.Length() tries to call 'Length' as a method, causing an error.
  3. Final Answer:

    Using parentheses after 'Length' property causes an error -> Option D
  4. Quick Check:

    Properties do not use () [OK]
Hint: Properties never have () after them [OK]
Common Mistakes:
  • Adding () to properties
  • Thinking Get-Item can't get file info
  • Believing Write-Output can't print variables
5. You want to list all running processes and display their names and memory usage using PowerShell objects. Which script correctly uses object properties to do this?
hard
A. Get-Process | ForEach-Object { '$_.Name uses $_.WorkingSet64 bytes' }
B. Get-Process | ForEach-Object { "$_.Name uses $_.WorkingSet64() bytes" }
C. Get-Process | ForEach-Object { $_.Name + ' uses ' + $_.WorkingSet64 + ' bytes' }
D. Get-Process | ForEach-Object { $_->Name + ' uses ' + $_->WorkingSet64 + ' bytes' }

Solution

  1. Step 1: Understand property access and string concatenation

    Properties are accessed with dot notation without parentheses. Concatenate strings with + in PowerShell.
  2. Step 2: Analyze each option

    A uses quotes with variables inside strings incorrectly (no expansion). B uses () on property causing error. C uses invalid arrow notation. D correctly concatenates properties and strings.
  3. Final Answer:

    Get-Process | ForEach-Object { $_.Name + ' uses ' + $_.WorkingSet64 + ' bytes' } -> Option C
  4. Quick Check:

    Use dot notation and + for string concat [OK]
Hint: Use dot notation and + for strings, no () or arrows [OK]
Common Mistakes:
  • Using () after properties
  • Using arrow notation -> which is invalid
  • Expecting variables to expand inside single quotes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard