# Adding a .Net object to a PSObject

**URL:** <https://forums.powershell.org/t/adding-a-net-object-to-a-psobject/479>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/adding-a-net-object-to-a-psobject/479 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/adding-a-net-object-to-a-psobject/479/1 "2011-12-31T18:30:00Z")

</div>

by dunagh at 2013-01-10 16:28:20

> Is it possible, and if so how does one, add a .Net object to to a PSObject. Specifically, I am trying to add a [Microsoft.Windows.Win32.RegistryKey] to a custome PSObject. I am finding that by doing so, it is converting it to as string because I am incorrectly adding it as a NoteProperty. The rundown of what I am trying to do is this:  
>   
> `$usersObject = @()$users = @("Sam Jones", "Frank Barber", "Sally Mae")$computer = "SystemX"foreach($user in $users){ $userObject = New-Object PSObject $userObject | Add-Member NoteProperty "UserNameName" $user $userObject | Add-Member NoteProperty "LoginID" $user.Replace(" ", "") $userObject | Add-Member NoteProperty "SID" "S-1-22-15-123412351-14513451-234562345" $userObject | Add-Member NoteProperty "UserReg" [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $computer) $usersObject += $userObject}$computerObject = New-Object PSObject$computerObject | Add-Member NoteProperty "ComputerName" "SystemX"$computerObject | Add-Member NoteProperty "Users" $usersObjectWrite-Output $computerObject`  
> … and I want $userObject.UserReg to contain a valid Microsoft.Win32.RegistryKey object.  
>   
> I am also afraid I am adding the $usersObject incorrectly to the $computerObject. Any help is appricated.  
>   
> Thanks.

by nohandle at 2013-01-11 02:56:52

> If you add the correct parentheses in the Add-Member NoteProperty "UserReg" clause it seems to work and produce what you need. (I used another static function from the same class to make the exaple work without using remote registry edit\> on the second thought I could have used localhost.🙂 )  
> `$usersObject = @()$users = @("Sam Jones", "Frank Barber", "Sally Mae") $computer = "SystemX" foreach($user in $users){ $userObject = New-Object PSObject $userObject | Add-Member NoteProperty "UserNameName" $user $userObject | Add-Member NoteProperty "LoginID" $user.Replace(" ", "") $userObject | Add-Member NoteProperty "SID" "S-1-22-15-123412351-14513451-234562345" $userObject | Add-Member NoteProperty "UserReg" ([Microsoft.Win32.RegistryKey]]]::Default)) $usersObject += $userObject}$user = $usersObject[0]$user.UserReg.GetType().FullName$user | Get-Member userreg`  
> `Microsoft.Win32.RegistryKey TypeName: System.Management.Automation.PSCustomObjectName MemberType Definition ---- ---------- ---------- UserReg NoteProperty Microsoft.Win32.RegistryKey UserReg=HKEY_CURRENT_USER`  
>   
> I also tried to just create an empty property, assign it a custom object and change to object to see if the changes propagate correctly and they did.   
> `$base = New-Object -TypeName PSObject -Property @{reference = ''}$property = New-Object -TypeName PsObject -Property @{name = 'john'}#add the object to the reference property$base.reference = $property$base.reference.name#change the original object$property.name = 'Sarah'#review the changes and type$base.reference.name $base.reference | gm`

by dunagh at 2013-01-16 16:50:43

> I had realized I was trying to unnecessarily cast the registry object, which I assign somewhere else in my script, as a Microsoft.Win32.RegistryKey object. When trying to cast the object while assigning it to the PSObject property, it saw it as a string. Thanks for the tip on putting parenthecies around an object though. I will keep that in mind if I need it in the future.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:02pm UTC](https://forums.powershell.org/t/adding-a-net-object-to-a-psobject/479/2 "2024-05-16T21:02:31Z")

</div>

Closed on May 16, 2024, 9:02pm UTC


