A ScriptMethod is a way to add a custom method to an object in PowerShell using a script block. It lets you extend objects with new behaviors.
Click to reveal answer
beginner
How do you add a ScriptMethod to an existing object in PowerShell?
Use the Add-Member cmdlet with the -MemberType ScriptMethod parameter, specifying the method name and a script block for the method's code.
Click to reveal answer
beginner
Example: What does this code do?
$person = New-Object PSObject -Property @{Name='Anna'}
$person | Add-Member -MemberType ScriptMethod -Name Greet -Value { "Hello, $($this.Name)!" }
$person.Greet()
It creates a new object with a Name property 'Anna'. Then it adds a method called Greet that returns a greeting string using the Name. Calling $person.Greet() outputs: Hello, Anna!
Click to reveal answer
intermediate
Why use $this inside a ScriptMethod in PowerShell?
$this refers to the current object the method is attached to. It lets the method access the object's properties and other data.
Click to reveal answer
beginner
Can you add multiple ScriptMethods to the same object?
Yes, you can add as many ScriptMethods as you want to an object using Add-Member. Each method can have its own name and script block.
Click to reveal answer
Which cmdlet is used to add a ScriptMethod to an object in PowerShell?
ANew-Method
BAdd-Member
CSet-Method
DInvoke-Method
✗ Incorrect
Add-Member is the cmdlet used to add properties or methods, including ScriptMethods, to objects.
In a ScriptMethod, what does $this represent?
AThe method's parameters
BThe global scope
CThe current object the method belongs to
DThe script file
✗ Incorrect
$this refers to the object instance that owns the ScriptMethod, allowing access to its properties.
What type of member do you specify to Add-Member to add a method with a script block?
AScriptMethod
BAliasProperty
CNoteProperty
DScriptProperty
✗ Incorrect
ScriptMethod is the member type used to add a method defined by a script block.
What will this code output?
$car = New-Object PSObject -Property @{Make='Ford'}
$car | Add-Member -MemberType ScriptMethod -Name ShowMake -Value { "Make is $($this.Make)" }
$car.ShowMake()
AMake is Ford
BMake is $this.Make
CError: Method not found
DMake is
✗ Incorrect
The ScriptMethod accesses the Make property via $this and returns 'Make is Ford'.
Can ScriptMethods access properties of the object they are added to?
AOnly if properties are global
BNo, they are independent
COnly if passed as parameters
DYes, using $this
✗ Incorrect
ScriptMethods use $this to access the object's properties and data.
Explain how to add a custom method to a PowerShell object using ScriptMethod.
Think about how you tell PowerShell to add a new behavior to an object.
You got /5 concepts.
Describe the role of $this inside a ScriptMethod in PowerShell and why it is important.
Consider how a method knows which object's data to use.
You got /4 concepts.
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?