Bird
Raised Fist0
PowerShellscripting~20 mins

Adding methods with ScriptMethod in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ScriptMethod Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell script adding a ScriptMethod?
Consider this PowerShell code that adds a ScriptMethod to a custom object. What will be the output when calling the method?
PowerShell
$obj = New-Object PSObject -Property @{Name='Alice'}
$scriptBlock = { "Hello, $($this.Name)!" }
$obj | Add-Member -MemberType ScriptMethod -Name Greet -Value $scriptBlock
$obj.Greet()
AHello, Alice!
BHello, $this.Name!
CHello, Name!
DError: Method not found
Attempts:
2 left
💡 Hint
Remember that $this inside a ScriptMethod refers to the object itself.
📝 Syntax
intermediate
2:00remaining
Which option correctly adds a ScriptMethod to a PowerShell object?
You want to add a ScriptMethod named 'SayBye' to a PSObject that returns 'Goodbye, !'. Which code snippet is correct?
A$obj | Add-Member -MemberType ScriptMethod -Name SayBye -Value { "Goodbye, this.Name!" }
B$obj | Add-Member -MemberType ScriptMethod -Name SayBye -Value { "Goodbye, $Name!" }
C$obj | Add-Member -MemberType ScriptMethod -Name SayBye -Value { "Goodbye, $this->Name!" }
D$obj | Add-Member -MemberType ScriptMethod -Name SayBye -Value { "Goodbye, $($this.Name)!" }
Attempts:
2 left
💡 Hint
Inside ScriptMethod, use $this to refer to the object itself.
🔧 Debug
advanced
2:00remaining
Why does this ScriptMethod fail to access the object's property?
Given this code, why does calling $obj.Hello() produce an error? $obj = New-Object PSObject -Property @{Name='Bob'} $sb = { "Hi, $Name!" } $obj | Add-Member -MemberType ScriptMethod -Name Hello -Value $sb $obj.Hello()
ABecause $Name is not defined inside the script block; should use $this.Name
BBecause Add-Member requires -PassThru to work with ScriptMethod
CBecause PSObject cannot have ScriptMethods added
DBecause the script block must be a string, not a script block
Attempts:
2 left
💡 Hint
Think about how to access object properties inside ScriptMethod script blocks.
🚀 Application
advanced
2:00remaining
How to add a ScriptMethod that modifies an object property?
You want to add a ScriptMethod 'SetAge' to a PSObject that sets its 'Age' property to a given value. Which code correctly does this?
A$obj | Add-Member -MemberType ScriptMethod -Name SetAge -Value { $Age = $args[0] }
B$obj | Add-Member -MemberType ScriptMethod -Name SetAge -Value { param($a) $this.Age = $a }
C$obj | Add-Member -MemberType ScriptMethod -Name SetAge -Value { param($a) $Age = $a }
D$obj | Add-Member -MemberType ScriptMethod -Name SetAge -Value { param($a) this.Age = $a }
Attempts:
2 left
💡 Hint
Use $this to refer to the object and param() to accept arguments.
🧠 Conceptual
expert
2:00remaining
What happens if you add multiple ScriptMethods with the same name to a PSObject?
If you add two ScriptMethods named 'Action' to the same PSObject, what will be the result when calling $obj.Action()?
ABoth methods run sequentially when calling $obj.Action()
BThe last added ScriptMethod overwrites the previous one; calling $obj.Action() runs the last method
CPowerShell throws an error when adding a duplicate ScriptMethod
DCalling $obj.Action() runs the first added method, ignoring the second
Attempts:
2 left
💡 Hint
Add-Member throws an error when adding a member with a name that already exists unless -Force is specified.

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

  1. Step 1: Understand Add-Member usage

    Add-Member is used to add properties or methods to existing objects in PowerShell.
  2. Step 2: Focus on ScriptMethod type

    When -MemberType ScriptMethod is used, it adds a method (a function) that can be called on the object.
  3. Final Answer:

    Adds a custom method (function) to an existing object -> Option B
  4. 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'?
easy
A. $obj | Add-Member -MemberType ScriptMethod -Name Greet -Value { 'Hello' }
B. Add-Member -InputObject $obj -MemberType ScriptProperty -Name Greet -Value { 'Hello' }
C. $obj.AddMember('Greet', { 'Hello' })
D. Add-Member -InputObject $obj -MemberType ScriptMethod -Name Greet -Value 'Hello'

Solution

  1. Step 1: Identify correct Add-Member syntax

    The cmdlet requires piping the object or using -InputObject, specifying -MemberType ScriptMethod, -Name, and a script block for -Value.
  2. Step 2: Check each option

    $obj | Add-Member -MemberType ScriptMethod -Name Greet -Value { 'Hello' } correctly pipes $obj and uses a script block { 'Hello' } as the method body. Add-Member -InputObject $obj -MemberType ScriptProperty -Name Greet -Value { 'Hello' } uses ScriptProperty, which is wrong. $obj.AddMember('Greet', { 'Hello' }) is invalid syntax. Add-Member -InputObject $obj -MemberType ScriptMethod -Name Greet -Value 'Hello' passes a string instead of a script block.
  3. Final Answer:

    $obj | Add-Member -MemberType ScriptMethod -Name Greet -Value { 'Hello' } -> Option A
  4. Quick Check:

    ScriptMethod needs script block value [OK]
Hint: Use a script block { } for ScriptMethod -Value [OK]
Common Mistakes:
  • Using ScriptProperty instead of ScriptMethod
  • Passing string instead of script block
  • Wrong cmdlet syntax without piping or -InputObject
3. What will be the output of this PowerShell code?
$obj = New-Object PSObject -Property @{Name='Alice'}
$obj | Add-Member -MemberType ScriptMethod -Name SayName -Value { "My name is $($this.Name)" }
$obj.SayName()
medium
A. My name is
B. My name is $this.Name
C. My name is Alice
D. Error: Property 'Name' not found

Solution

  1. Step 1: Understand object and method

    $obj has a property Name='Alice'. The ScriptMethod SayName uses $this.Name to access that property.
  2. Step 2: Evaluate method call output

    Calling $obj.SayName() runs the script block, replacing $($this.Name) with 'Alice', so output is 'My name is Alice'.
  3. Final Answer:

    My name is Alice -> Option C
  4. Quick Check:

    $this.Name accesses property correctly [OK]
Hint: Use $this to access object properties inside ScriptMethod [OK]
Common Mistakes:
  • Expecting literal $this.Name output
  • Confusing property access syntax
  • Thinking method won't access object properties
4. Identify the error in this code snippet that tries to add a ScriptMethod to $obj:
$obj = New-Object PSObject -Property @{Age=30}
Add-Member -InputObject $obj -MemberType ScriptMethod -Name GetAge -Value { return $Age }
$obj.GetAge()
medium
A. The property Age does not exist on $obj
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

  1. 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.
  2. Step 2: Verify other parts

    Using -InputObject is valid. ScriptMethod can return values. Property Age exists on $obj.
  3. Final Answer:

    The script block should use $this.Age to access the property -> Option D
  4. 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?
hard
A. $person | Add-Member -MemberType ScriptMethod -Name FullName -Value { "$($this.FirstName) $($this.LastName)" }
B. $person | Add-Member -MemberType ScriptProperty -Name FullName -Value { "$($this.FirstName) $($this.LastName)" }
C. $person | Add-Member -MemberType ScriptMethod -Name FullName -Value { $this.FirstName + $this.LastName }
D. $person | Add-Member -MemberType ScriptMethod -Name FullName -Value { return "$this.FirstName $this.LastName" }

Solution

  1. Step 1: Confirm correct MemberType and method body

    The method must be a ScriptMethod, not ScriptProperty.
  2. Step 2: Check string concatenation and property access

    $person | Add-Member -MemberType ScriptMethod -Name FullName -Value { "$($this.FirstName) $($this.LastName)" } uses string interpolation with $($this.FirstName) and $($this.LastName) separated by space, which correctly returns full name.
  3. Step 3: Analyze other options

    $person | Add-Member -MemberType ScriptMethod -Name FullName -Value { $this.FirstName + $this.LastName } concatenates without space. $person | Add-Member -MemberType ScriptMethod -Name FullName -Value { return "$this.FirstName $this.LastName" } uses return but misses $() around variables, so variables won't expand properly.
  4. Final Answer:

    $person | Add-Member -MemberType ScriptMethod -Name FullName -Value { "$($this.FirstName) $($this.LastName)" } -> Option A
  5. Quick Check:

    Use string interpolation with $() for properties [OK]
Hint: Use "$($this.Prop) $($this.Prop)" for spaced string in ScriptMethod [OK]
Common Mistakes:
  • Using ScriptProperty instead of ScriptMethod
  • Concatenating strings without space
  • Not using $() for property expansion inside strings
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