3

I'm trying to make a collection of custom objects in powershell and store them in a hashtable. The problem is that the custom attributes disapear when I put the object into a hashtable.

$customObject = New-Object object

$customObject | Add-member -membertype noteproperty -name customValue -value "test"

Write-Host $customObject.customValue

$hashTable = @{}

$hashTable.add("custom", $customObject)

$object = $hashTable["custom"]

$object.customValue = 7

When I execute this code I get the following output.

test
Property 'customValue' cannot be found on this object; make sure it exists and is settable.
At C:\temp\test2.ps1:15 char:9
+ $object. <<<< customValue = 7
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Is there some way I can use the custom attribute after I've placed it into a collection?

2
  • 1
    The code works for me. What PowerShell version are you using? Commented Aug 14, 2013 at 18:42
  • I'm using powershell 2.0 Commented Aug 14, 2013 at 22:01

1 Answer 1

4

I am using PowerShell 3.0 on 64-bit Windows 7. In 3.0 your code runs as expected, but in 2.0 (powershell.exe -Version 2.0) I get the same error as you. What's really strange is this output under 2.0:

PS> [Object]::ReferenceEquals($customObject, $object)
True
PS> $customObject | Get-Member


   TypeName: System.Object

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
customValue NoteProperty System.String customValue=test


PS> $object | Get-Member


   TypeName: System.Object

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
ToString    Method     string ToString()

So, PowerShell agrees that they're the same object, yet only one has a customValue property. I also notice that if I change the way you are adding $customObject to $hashTable from this...

$hashTable.add("custom", $customObject)

...to this...

$hashTable["custom"] = $customObject

...then your code works as expected under PowerShell 2.0. So, it seems like something is going wrong in the call to Add(), and that behavior must have been fixed in 3.0.

Another workaround is to change the first line from this...

$customObject = New-Object object

...to this...

$customObject = New-Object PSObject

...and your code runs without error in both versions of PowerShell. You can then shorten the first two lines to this...

$customObject = New-Object PSObject -Property @{ customValue = "test" }
Sign up to request clarification or add additional context in comments.

1 Comment

Works great. I'm running powershell version 2.0

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.