0
0
PowerShellscripting~10 mins

Adding methods with ScriptMethod in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Adding methods with ScriptMethod
Create object
Define script block
Add ScriptMethod to object
Call method
Method executes script block
Output result
This flow shows how to create an object, add a ScriptMethod to it, then call that method to run the script block and get the output.
Execution Sample
PowerShell
$obj = New-Object PSObject
$scriptBlock = { "Hello, World!" }
$obj | Add-Member -MemberType ScriptMethod -Name Greet -Value $scriptBlock
$obj.Greet()
Creates an object, adds a ScriptMethod named Greet that returns 'Hello, World!', then calls the method.
Execution Table
StepActionVariable StateOutput
1Create new PSObject assigned to $obj$obj = PSObject (empty)
2Define script block returning 'Hello, World!'$scriptBlock = script block
3Add ScriptMethod 'Greet' to $obj with $scriptBlock$obj has method Greet
4Call $obj.Greet()$obj unchangedHello, World!
5End of script$obj unchanged
💡 Script ends after calling the ScriptMethod and outputting the result.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
$objundefinedPSObject (empty)PSObject (empty)PSObject with ScriptMethod GreetPSObject with ScriptMethod GreetPSObject with ScriptMethod Greet
$scriptBlockundefinedundefinedScript block returning 'Hello, World!'Script block returning 'Hello, World!'Script block returning 'Hello, World!'Script block returning 'Hello, World!'
Key Moments - 3 Insights
Why do we use a script block when adding a ScriptMethod?
The script block holds the code that runs when the method is called. See execution_table step 2 and 3 where the script block is defined and then added as a method.
Does adding a ScriptMethod change the object’s existing properties?
No, it only adds a new method. The object’s other properties stay the same. See variable_tracker for $obj before and after adding the method.
What happens when we call the ScriptMethod?
The script block runs and returns its output. See execution_table step 4 where calling $obj.Greet() outputs 'Hello, World!'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What does $obj have after adding the ScriptMethod?
AA new property named Greet
BA new method named Greet
CThe script block removed
DNo changes
💡 Hint
Check the 'Variable State' column at step 3 in the execution_table.
At which step does the script block get defined?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column describing defining the script block.
If we change the script block to return 'Hi!', what will be the output at step 4?
AHi!
BNo output
CHello, World!
DError
💡 Hint
The output depends on what the script block returns when the method is called.
Concept Snapshot
Add methods to objects using ScriptMethod.
Define a script block with the method code.
Use Add-Member -MemberType ScriptMethod to add it.
Call the method like a normal function.
The script block runs and returns output.
Full Transcript
This example shows how to add a method to a PowerShell object using ScriptMethod. First, we create a new PSObject and store it in $obj. Then, we define a script block that returns the string 'Hello, World!'. Next, we add this script block as a ScriptMethod named Greet to the $obj using Add-Member. Finally, we call $obj.Greet(), which runs the script block and outputs 'Hello, World!'. The object itself does not change except for having the new method added. This method behaves like a function attached to the object.