To answer the question generically (even though the specific use case may warrant a simpler solution, as demonstrated in TheIncorrigible1's answer):
Note: In the code below I'm assuming that by $test.mymethod('key1')='value1' you actually meant to say that you want $test.mymethod('key1') to return 'value1', given that using a method call as the LHS of an assignment doesn't make sense.
lit mentions a PSv5+ alternative: defining a [class]:
# Define the class.
class Dummy {
[string] mymethod([string] $key) { return @{ key1='value1'; key2='value2' }[$key] }
}
# Instantiate it
$test = [Dummy]::new()
# Call the method
$test.mymethod('key2') # -> 'value2'
If you do want to use PowerShell's ETS (extended type system), as in your question (which is your only option in PSv4-, short of embedded C# code via Add-Type):
Perhaps the only hurdle was not knowing how to define a parameter for the method (use a param() block) and possibly how to access the instance on which the method is invoked (use $this); both techniques are demonstrated below:
# Define a type named 'Dummy' and attach a script method named 'mymethod'
$mymethod = @{
MemberName = 'mymethod'
MemberType = 'ScriptMethod'
# Note the use of param() to define the method parameter and
# the use of $this to access the instance at hand.
Value = { param([string] $key) $this.Dict[$key] }
Force = $true
}
Update-TypeData -TypeName 'Dummy' @mymethod
# Create a custom object and give it an ETS type name of 'Dummy', which
# makes the mymethod() method available.
$test = [PsCustomObject] @{ PsTypeName = 'Dummy'; Dict = @{ key1='value1'; key2='value2' } }
$test.mymethod('key2') # -> 'value2'
classwhich would enable you to create instance objects with methods. What is the output of$PSVersionTable.PSVersion?$test = [PSCustomObject]@{'foo' = @{}}; $test.foo['key1'] = 'value1'.[ordered]before the hashtable literal (@{ ... }) that you cast to[pscustomobject], because[pscustomobject] { ... }is syntactic sugar that implicitly preserves the key-definition order in the resulting object's ordering of properties.