Add, remove, or replace values using PowerShell

This is just a tiny blog post after a busy week. On Tuesday, it was time for our ‘TechDay’ event from my employer (NEXXT), an in-person event with our colleagues where you could give sessions for them. And… We hosted the WorkPlaceDudes meetup #3 community event on Thursday, which was great! If you can join the next edition of the meetup, subscribe to the event using Meetup. (It’s a non-online event in the Netherlands, but presentation slides will be shared on this page afterward.)

I had another encounter this week where a customer wanted to remove the value from something in Exchange. Setting the value to $null didn’t work, and I showed him how to remove it another way. This blog post will show a few ways to add, remove, or replace a value using Active Directory and Exchange Online as examples.

Active Directory

In the examples below, I will show you how to Add, Remove, or replace a value of an Active Directory Attribute using the parameters provided by the Active Directory cmdlets.

Creating a test user

I created the account User1 in my test.local active Directory for testing, as shown below, including the otherTelephone Attribute which we will use for testing:

Get-ADUser -Identity User1 -Properties otherTelephone
DistinguishedName : CN=User 1,OU=Users,OU=Corp,DC=test,DC=local
Enabled           : True
GivenName         : User
Name              : User 1
ObjectClass       : user
ObjectGUID        : 1ed67609-47f1-407b-acf4-367d9b077d96
otherTelephone    : {12345678}
SamAccountName    : user1
SID               : S-1-5-21-3052270451-3245113913-2344164184-1111
Surname           : 1
UserPrincipalName : user1@test.local

Adding a single value to an object

If we want to add a value to the otherTelephone attribute of User1, for example, you can use the Add parameter like this:

Set-ADUser -Identity $user -Add @{otherTelephone='87654321'}   
Get-ADUser -Identity User1 -Properties otherTelephone       


DistinguishedName : CN=User 1,OU=Users,OU=Corp,DC=test,DC=local
Enabled           : True
GivenName         : User
Name              : User 1
ObjectClass       : user
ObjectGUID        : 1ed67609-47f1-407b-acf4-367d9b077d96
otherTelephone    : {87654321, 12345678}
SamAccountName    : user1
SID               : S-1-5-21-3052270451-3245113913-2344164184-1111
Surname           : 1
UserPrincipalName : user1@test.local

As you can see from the output above, the otherTelephone attribute now contains two values (87654321 and 12345678)

Removing a single value from an object

If we want to remove a value to the otherTelephone attribute of User1, for example, you can use the Remove parameter like this:

Set-ADUser -Identity $user -Remove @{otherTelephone='12345678'}                  
Get-ADUser -Identity User1 -Properties otherTelephone


DistinguishedName : CN=User 1,OU=Users,OU=Corp,DC=test,DC=local
Enabled           : True
GivenName         : User
Name              : User 1
ObjectClass       : user
ObjectGUID        : 1ed67609-47f1-407b-acf4-367d9b077d96
otherTelephone    : {87654321}
SamAccountName    : user1
SID               : S-1-5-21-3052270451-3245113913-2344164184-1111
Surname           : 1
UserPrincipalName : user1@test.local

As you can see from the output above, the otherTelephone attribute now contains one value again (8765432).

Replacing a value from an object

You can also replace all the values with other ones by using:

Set-ADUser -Identity $user -Replace @{otherTelephone='456123','876123'}
Get-ADUser -Identity User1 -Properties otherTelephone


DistinguishedName : CN=User 1,OU=Users,OU=Corp,DC=test,DC=local
Enabled           : True
GivenName         : User
Name              : User 1
ObjectClass       : user
ObjectGUID        : 1ed67609-47f1-407b-acf4-367d9b077d96
otherTelephone    : {876123, 456123}
SamAccountName    : user1
SID               : S-1-5-21-3052270451-3245113913-2344164184-1111
Surname           : 1
UserPrincipalName : user1@test.local

As you can see, the previous value, 87654321, is gone and has been replaced by 876123 and 456123.

Exchange Online

In the examples below, I will show you how to Add, Remove, or replace a value of an Exchange Online user using the Exchange Online Management cmdlets.

Adding a single value to the EmailAddresses object

You can use the Set-Mailbox cmdlet to add the new email test@test.com address to an existing mailbox. For example:

Set-Mailbox -Identity powershell@test.com -EmailAddresses @{Add='test@test.com'}

Get-Mailbox -Identity powershell@test.com | Select-Object -ExpandProperty emailaddresses
smtp:test@test.com
SIP:powershell@test.com
SMTP:powershell@test.com
smtp:powershell@test.onmicrosoft.com

Removing a single value from the EmailAddresses object

To remove a value, you can also use the Set-Mailbox cmdlet again like this to remove the test@test.com email address from the powershell@test.com mailbox:

Set-Mailbox -Identity powershell@test.com -EmailAddresses @{Remove='test@test.com'}

Get-Mailbox -Identity powershell@test.com | Select-Object -ExpandProperty emailaddresses
SIP:powershell@test.com
SMTP:powershell@test.com
smtp:powershell@test.onmicrosoft.com

Replacing a value from the EmailAddresses object

You can also replace all the values with other ones for the powershell@test.com mailbox by using:

Set-Mailbox -Identity powershell@test.com -EmailAddresses SMTP:powershell@test.com, smtp:test2@test.com, smtp:powershell@test.onmicrosoft.com, SIP:powershell@test.com

Get-Mailbox -Identity powershell@test.com | Select-Object -ExpandProperty emailaddresses
SIP:powershell@test.com
SMTP:powershell@test.com
smtp: test2@test.com
smtp:powershell@test.onmicrosoft.com

(There is no replace option like you might expect after using Add and Remove)

Wrapping up

In the examples above, I showed you how to change a multi-valued object using Parameters to Add, Remove, or Replace values and how to do that using the @{Add=} or @{Remove=} way. But always check if these options are available for other cmdlets. (The @{Add/Remove} manner will not work for PSCustomObjects, for example…)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.