0

I have a custom powershell object created with a function:

$obj = myfunction -name "hello" -value 5

After it's created I want to change the value property however doing it (demonstrated below) doesn't work

$obj.value = 1

I've searched and can't seem to find anything - can anybody explain how I can accomplish this?

Here is my function that creates an returns the object

function myfunction
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    Param
    (
        [Parameter(Mandatory=$true,
               Position=0)]
        [String]
        $name,

        [Parameter(Mandatory=$true,
               Position=1)]
        [int]
        $value,
    )
    Process
    {
        $myfunction = @{
            name = $name
            value = $value
        }
        write-output $myfunction
    }
}
4
  • I doubt that you have a "Custom PowerShell Object" (where everyone reads that as a "PSCustomObject" -- as highlighted by Jaqueline below). You must have some sort of .Net class which has a readonly property. Can you run $obj.GetType().FullName Commented Dec 23, 2015 at 23:24
  • I've added the function definition Commented Dec 23, 2015 at 23:25
  • If that's your function, I can't see why your other code would not work. You're returning a hashtable, and in PowerShell, hashtables CAN be accessed the way you wrote. Commented Dec 23, 2015 at 23:33
  • Are you able to reproduce your problem? What is your PowerShell version? Else this should probably be closed. Commented Dec 23, 2015 at 23:47

3 Answers 3

3

If you're returning a hashtable, you should be able to do what you wrote:

PS> $obj = myfunction -name "hello" -value 5
PS> $obj.value = 1
PS> $obj

Name                           Value
----                           -----
name                           hello
value                          1

However, the "safe" way is to use square braces.

You might want to try that, because my gut is that you tried to set .Values instead of .Value ... and .Values is a non-settable property of Hashtables.

PS> $obj = myfunction -name "hello" -value 5
PS> $obj["value"] = 1
PS> $obj

Name                           Value
----                           -----
name                           hello
value                          1

Either way, you could avoid all that by creating an actual PSCustomObject as @jaqueline-vanek did in her answer.

Sign up to request clarification or add additional context in comments.

Comments

2
PS C:\Users\joshua> $obj = [PSCustomObject]@{ hello = 5 }
PS C:\Users\joshua> $obj.hello
5
PS C:\Users\joshua> $obj.hello = 1
PS C:\Users\joshua> $obj.hello
1

PowerShell: Creating Custom Objects

2 Comments

Please mention where in the OP's code to put the [PSCustomObject] to make it work with their function.
it's not about the type but the technet article :)
0

The OP is correct it does not work. It should be,

$obj."Hello" to access the property value

$obj."Hello" = 1 to set the property.

You have to double quote the property name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.