📖 Scenario: You are managing a list of simple objects representing books in a library. Each book has a title and an author. You want to add a method to each book object that can display its details in a friendly format.
🎯 Goal: Learn how to add a method called ShowDetails to each book object using ScriptMethod. This method will print the book's title and author in a readable sentence.
📋 What You'll Learn
Create a list of book objects with Title and Author properties
Add a ScriptMethod called ShowDetails to each book object
The ShowDetails method should output a string like: 'Title: [Title], Author: [Author]'
Call the ShowDetails method for each book and display the output
💡 Why This Matters
🌍 Real World
Adding methods to objects dynamically is useful when working with data that needs custom behavior without creating full classes. For example, enhancing data from APIs or files.
💼 Career
PowerShell scripting with custom objects and methods is common in system administration and automation tasks, helping to organize and extend data processing.
Progress0 / 4 steps
1
Create the list of book objects
Create a variable called books that contains two custom objects. The first object should have Title set to 'The Hobbit' and Author set to 'J.R.R. Tolkien'. The second object should have Title set to '1984' and Author set to 'George Orwell'.
PowerShell
Hint
Use [PSCustomObject] to create each book with Title and Author properties inside an array.
2
Add the ScriptMethod to each book
Add a ScriptMethod called ShowDetails to each book object in $books. The method should return a string formatted as 'Title: [Title], Author: [Author]' using the object's properties.
PowerShell
Hint
Use a foreach loop to add a PSScriptMethod named ShowDetails to each book's PSObject.Members.
3
Call the ShowDetails method for each book
Use a foreach loop to call the ShowDetails method on each book object in $books and store the results in a new list called details.
PowerShell
Hint
Use a foreach loop to call ShowDetails() on each book and collect the output in $details.
4
Display the details of all books
Print the contents of the $details list to show the formatted details of each book.
PowerShell
Hint
Use Write-Output or Write-Host to print each string in $details.
Practice
(1/5)
1. What does the Add-Member -MemberType ScriptMethod cmdlet do in PowerShell?
easy
A. Deletes a property from an object
B. Adds a custom method (function) to an existing object
C. Creates a new object from scratch
D. Converts an object to a string
Solution
Step 1: Understand Add-Member usage
Add-Member is used to add properties or methods to existing objects in PowerShell.
Step 2: Focus on ScriptMethod type
When -MemberType ScriptMethod is used, it adds a method (a function) that can be called on the object.
Final Answer:
Adds a custom method (function) to an existing object -> Option B
Quick Check:
Add-Member ScriptMethod = Adds method [OK]
Hint: ScriptMethod adds functions to objects, not properties [OK]
Common Mistakes:
Confusing ScriptMethod with adding properties
Thinking it creates new objects
Assuming it deletes object members
2. Which of the following is the correct syntax to add a ScriptMethod named Greet to an object $obj that returns 'Hello'?
B. Add-Member requires piping the object, not -InputObject
C. ScriptMethod cannot return values
D. The script block should use $this.Age to access the property
Solution
Step 1: Check property access inside ScriptMethod
The script block uses $Age which is undefined in this scope. To access the object's property, use $this.Age.
Step 2: Verify other parts
Using -InputObject is valid. ScriptMethod can return values. Property Age exists on $obj.
Final Answer:
The script block should use $this.Age to access the property -> Option D
Quick Check:
Use $this.Property inside ScriptMethod [OK]
Hint: Inside ScriptMethod, always use $this.Property to access object data [OK]
Common Mistakes:
Using property name without $this inside script block
Thinking piping is mandatory for Add-Member
Assuming ScriptMethod can't return values
5. You want to add a ScriptMethod FullName to an object $person that has properties FirstName and LastName. The method should return the full name with a space between first and last names. Which code correctly implements this?