Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Appending Property Values in PowerShell

Posted on December 16, 2009

This morning I helped out a fellow scripter in the PowerShell forum at ScriptingAnswers.com. He was trying to figure out an Exchange 2007 problem. He wanted to update a property value, but keep the existing property values. This seems a like a reasonable request and one that isn’t limited to Exchange. There are plenty of objects you might work with in PowerShell where you want to keep the existing property value and add to it. My solution is specific to the Exchange problem, but I think you could use it as a model for similar problems with other objects.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

In Exchange you can grant SendOnBehalf privileges for a mailbox using the Set-Mailbox cmdlet and the –GrantSendOnBehalfTo parameter.

PS C:\> get-mailbox jeff | set-mailbox –grantSendOnBehalfto beth

This will add the user “beth” the appropriate privilege. The parameter requires a mailbox identity string like the user ‘s SAMAccountname or distinguishedname. The Jeff mailbox’s GrantSendOnBehalfTo property is now an ADInfo object with its own set of properties. The Set-Mailbox cmdlet also lets you specify multiple values for this parameter.

PS C:\> get-mailbox jeff | set-mailbox –grantSendOnBehalfto beth,lucky

The challenge was to keep the existing values. Here’s the solution I offered.

   1: get-mailbox test.user24 | foreach {

   2:   $a=@() 

   3:   #enumerate existing values

   4:   foreach ($granted in $_.GrantSendOnBehalfTo) {

   5:     $a+=$granted.distinguishedname

   6:   }

   7:   #add the new user 

   8:   $a+="fflintstone"

   9:  

  10:   $_ | set-mailbox -grantSendonBehalfTo $a

  11: }

I’m doing this for a single mailbox, but it it would work for a collection as well. The trick, if you will, is that you need to work with each mailbox individually so I pipe the mailbox to ForEach on Line 1.

Because the Set-Mailbox cmdlet has no way to append properties, I need to save the existing values for the GrantSendonBehalfTo property. To accomplish this I’ll initialize and empty array in Line 2. The property may have a collection of values or objects, so I need to enumerate each one as I do on Line 3. Remember, in this case each property value is an embedded object. I want to save the distinguishedname of each embedded object to the array (Line 5). Finally I need to add the name of the new user I want (Line 8). I can keep it simple by using the SAMAccountname. All that remains is to set the property using Set-Mailbox, using the array as the value. The cmdlet handles parsing the array accordingly. This works because the –GrantSendonBehalfTo parameter accepts arrays.

-GrantSendOnBehalfTo <MailboxOrMailUserIdParameter[]>

    The GrantSendOnBehalfTo parameter specifies the distinguished name (DN)

     of other mailboxes that can send messages on behalf of this mailbox.

 

    Required?                    false

    Position?                    Named

    Default value                Null

    Accept pipeline input?       False

    Accept wildcard characters?  false

I can tell from reading the help that the is looking for a MailboxOrMailUserIDParameter and the {} indicates it can accept an a array of them.

When I look at the mailboxes GrantSendonBehalfTo property I have the previously set values plus my new one.

grantsendonbehalfto

I think you can use these principals and techniques with other objects. Remember that you likely have to work with objects individually using ForEach. Sure, you could likely simplify my code but this will likely take a scripted solution so you might as well make it meaningful.

If you come up with a similar solution for other types of objects or cmdlets I hope you’ll share.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Appending Property Values in PowerShell”

  1. Pingback: Tweets that mention Appending Property Values in PowerShell | The Lonely Administrator -- Topsy.com

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d