Adding object properties to another object in Powershell

powershell add property to object
new-object powershell
powershell add-member array
powershell add object to array
powershell add-member to existing array
powershell add-member multiple values
powershell json add-member
powershell custom object

I'm trying to add the properties of the object $mailBox_MBS to the object $mailBox_RP.

I used the following code to define the objects:

$mailBox_MBS = (Get-MailboxStatistics -Identity $identity) | select *
$mailBox_RP = (Get-Recipient -Identity $identity) | select *

Then I used the following ForEach loop to add the properties from mailBox_MBS to mailBox_RP:

Foreach($property in $mailbox_MBS)    
  $mailBox_RP | Add-Member -MemberType NoteProperty -Name $property.Name -Value $property.Value 

You can't do it the way you're trying to do. Your foreach will only see a single object and you're only catching the name and value properties. What you want to do is actually iterate the properties of an object. Keep in mind that you might loose object complexity while doing this (implicit conversations to strings). Especially with Exchange this can be a problem.

You could either create a custom PSObject to just contain what you need or you'd need to look into using Get-Member to get the actual object Information. That might look like this.

$object | Get-Member -MemberType Property | %{ $object.$($_.Name); }

Adding object properties to another object in Powershell, Since we can add anything to a custom object, from any other will add the properties or methods we specify to the object we create. We'll be� I’m using splatting to flow the -Passthru switch along to Add-Member; Add-Member allows a hashtable of property names and values, just like New-Object does. I learned that today! I’ve allowed pipeline input, but you only get one object to copy from 🙁 I could have called Add-Member for each property, but I like doing it this way better. I

Let me know if this works for you:

$mailbox_MBS = Get-MailboxStatistics -Identity $identity
$mailbox_RP = Get-Recipient -Identity $identity

Get-Member -InputObject $mailbox_MBS -MemberType "*Property" | foreach {
    Add-Member -InputObject $mailbox_RP -Type "NoteProperty" -Name $_.Name -Value $mailbox_MBS.($_.Name)

PowerShell: Getting Started, Each time you look at $Object you'll notice that the date property has a different value. As Martin noted, since the script block is evaluated each� We can use the calculated properties feature with the Select-Object cmdlet to change or even add additional object properties as PowerShell returns them. Select-Object is a common cmdlet typically used in PowerShell either to show all properties on an object or to limit the default properties shown.

I managed to convert the code above into a working function, with the help of Seth's comment and my best friend Google.

Function mailBoxInfo ($identity)
    $ExchangeDirectoryObject = New-Object PSObject
    $mailbox_MBS = (Get-MailboxStatistics -Identity $identity) | select *
    $mailbox_RP = (Get-Recipient -Identity $identity) | select *

$mailBox_MBS.psobject.Properties | % {
    $ExchangeDirectoryObject | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value
$mailBox_RP.psobject.Properties | % {
    $ExchangeDirectoryObject | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value

return $ExchangeDirectoryObject

[SOLVED] Adding members to an existing object, You will want to keep your object AND modify it by adding a property to it. in PowerShell to extend an existing object with a new property. Adding new values to custom object properties Welcome › Forums › General PowerShell Q&A › Adding new values to custom object properties This topic has 0 replies, 1 voice, and was last updated 8 years, 5 months ago by Forums Archives .

Ultimate Guide to Using PowerShell Add-Member Cmdlet, Add a user-defined custom member to a PowerShell object instance. Valid values for this are: {AliasProperty | CodeProperty| Property -force SwitchParameter Adds a new member even if one with the same name already exists. Does not� Type a reference to the object, such as a variable that contains the object, or a command that gets the object. Then, type a dot (.) followed by the property name. For example, the following command displays the value of the CreationTime property of the PowerShell.exe file.

Add-Member - PowerShell, I created a PSobject and added some properties and values. it looks like this: Now I want to add a second line with similar values to my object but it doesn't We have small WAN lines to some servers in China and several other countries. If you need to add a script method to an object, you can do it with Add-Member and a ScriptBlock. You have to use the this automatic variable reference the current object. Here is a scriptblock to turn a object into a hashtable. (same code form the last example)

adding new values to a psobject? - PowerShell General, You will discover that PowerShell is production-oriented. So, if you specify object properties that do not exist, you will simply get back "nothing": $� Add-Member will add to every object in the array, for example: Powershell $wmiParams = @{ 'ComputerName' = $computer 'Namespace' = 'ROOT\MicrosoftDfs' 'Class' = 'DfsrSyncInfo' 'Filter' = '' 'Property' = '*' } $instanceSet = ( Get-WmiObject @wmiParams ) $instanceSet | Add-Member -MemberType ScriptProperty -Name StartTimeFriendly -Value { convertDateTime $this .

  • Thanks for your reply, Seth. Look under the **Edit on my post. Is this what you mean by that? Unfortunately it's not working.
  • It was an either though certainly you could use that approach to create a custom object. Spend some time to check out what Get-Member does and check what your current code does. My example only shows a rough guideline and what you can do with Get-Member. It doesn't make sense that way to actually change an object. You'd need to combine it with your current approach. Currently you wouldn't even touch your original objects.
  • Thanks for your help Seth.
  • Thanks for this, I managed to solve it. Very nice to see a different way to approach what I was looking for.