How to Add, Remove and Modify Recipients in Exchange Server using PowerShell

Home > Blogs > Exchange Server > How to Add, Remove and Modify Recipients in Exchange Server using PowerShell

How to Add, Remove and Modify Recipients in Exchange Server using PowerShell

Like This Blog 0Mike Pfeiffer
Added by September 13, 2013

You’ll probably spend a lot of time in your Exchange Management Shell (EMS) on the recipients, creating mailboxes, modifying users and things like that. If you’re going to look at how you’re going to do this one at a time but also more powerful options for doing this in bulk. Taking things out of the CSV or text file and creating multiple users at one time. A lot of power here. We’ll also look at other interesting things here like creating inbox rules, programmatically through PowerShell, managing mailbox folder permissions and also managing out of office settings for users.

Here are a couple scenarios in where you’ll add, modify and remove recipients so let’s take a look at a few of those. First one is going to be creating mailboxes for users that are already out there in your active directory environment. So we look at get user and we’ll do recipient type details of user.

001-get-user-add-modify-exchange-server-powershell

That’s basically going to show you all the Active Directory users that don’t have mailboxes.

002-get-user-add-modify-exchange-server-powershell

I could do here is kind of figure out all right we’ve got this guy, Simon Lamb, we’ll use him. If we want to give his existing AD account or mailbox enable his existing AD account. We could just simply do an enable mailbox, SLamb.

003-enable-mailbox-add-modify-exchange-server-powershell

That would give him a mailbox. Now if we want to create a new AD user and a mailbox‑enabled user, we would use the new mailbox cmdlet.

That requires that you define a password for the user. I’ve shown this example previously but that value needs to be encrypted. So usually what I do for this is I use the ConvertTo-SecureString  cmdlet and then I pass this whatever the value is that I want to use for the password.

004-convertto-securestring-add-modify-exchange-server-powershell

So in this case, I’m converting this plain text password(-ASPlainText –Force) into a secure string.

005-convertto-securestring-add-modify-exchange-server-powershell

So you got to do an as point text. Then you got to do a force and if you echo that variable back, you can see that that’s the .NET framework type for the secure string class. Really what we’re saying here is we just don’t want to send this across the network when we’re creating the user. We want to do it encrypted. From there I can do a new mailbox and we can something like Kevin Jones, alias first initial, last name, user principle name and finally the password.

006-new-mailbox-convertto-securestring-add-modify-exchange-server-powershell

These are the required parameters with new mailbox. So these are the bare minimum to get user created. However this command has all kinds of different parameters so you can do all kinds of different things when you create the mailbox. For this I’m just going to go ahead and run that. You can see that it goes on and creates the user. So this is very static.

007-new-mailbox-convertto-securestring-add-modify-exchange-server-powershell

This is sitting in the shell one line at a time. You’re not probably going to add users this way. You might do this in bulk through some kind of provisioning script or you might have an interactive script where someone else is actually running that like maybe a help desk or junior admin. You might want to capture some of that information and prompt them along the way.

You may also like:  How to Remotely Enable and Disable (RDP) Remote Desktop

What I’m going to do is show an example of that. We’ll call this new mailbox.ps1

008--add-modify-exchange-server-powershell

The idea would be that an admin might run this. And admin might run this and be kind of prompted for information along the way. Initially we might want to prompt them for a password so we do a read hosts, enter the password and you would do this as a secure string.

009-secure-string-add-modify-exchange-server-powershell

So instead of manually creating that password object, you could prompt the user in the ISE if you run this.

010-ISE-secure-string-add-modify-exchange-server-powershell

You can see that it would actually give you a dialog box but if you run it in the Shell,

011-user-profile-string-add-modify-exchange-server-powershell

From here I’ll run new mailbox.ps1. There you can see that it’s prompting me.

012-user-profile-string-add-modify-exchange-server-powershell

It’s actually masking the password with stars. So that’s kind of nice. It’s also good for an interactive script.

If I go back to my script, that’s going to be the password then I might want the name. So we do another Read-Host “Enter the name”. This will not be a secure string we want that to be in plain text, $UPN= Read-Host “Enter the userprinciplename” Name. $alis = Read-Host “Enter an alias” . This should be good enough.

013-user-profile-string-add-modify-exchange-server-powershell

From there you can go out and run New-Mailbox and you can just pass these variables to the parameter values $Name.

The $ name is going to be captured from my user and user principle name same thing, same thing for the password. That’s $ alias. Now if I say that this is a script an operator could run that. You put out the script there somewhere on their machine where they can get to it. They don’t have to necessarily know all the details about PowerShell. They could run that. It would prompt them along the way to create the mailbox.

014-user-profile-string-add-modify-exchange-server-powershell

Let’s just run through that.

015-run-user-profile-string-add-modify-exchange-server-powershell

So I’m going to enter password and that’s going to get saved. We’ll do Jeff Jones this time. His UPN will be jjones.uss.local and his alias will be jjones and that creates the mailbox.

016-run-user-profile-string-add-modify-exchange-server-powershell

So that is one way you can do this and that’s probably just creating the mailbox, probably isn’t the only thing you need to do. You might need to set some attributes.

For example the new mailbox cmdlet doesn’t give you the ability to set the title. So it might be something that you have to do during the provisioning process. So you might want to prompt for that too. The $title, enter the title.

017-set-title-add-modify-exchange-server-powershell

That’s actually another command that you have to run. So you first have to create the mailbox then you would need to do a set user on that name and set the title to $title.

018-set-title-add-modify-exchange-server-powershell

Let’s go ahead and save that. We’ll run it again. Create a password. The name Jason Helmick for this user, jason.uss.local his alias can simply be jasonh and his title would be janitor.

019-set-title-add-modify-exchange-server-powershell

We run that and it creates the mailbox. Now if we run Get-User, it will set the title.

020-set-title-add-modify-exchange-server-powershell

So kind of a two‑step thing here. You can go to add the user after they’ve been created and you’ve set all their attributes. You could add them.

You may also like:  Using PowerShell to convert to the Full Graphical Shell on a Windows Server 2012 R2 Datacenter Core Edition

Do a distribution list ‑‑ all kinds of different things, fire off an email. So this is kind of the use case for an interactive script.

Now let’s take a look at removing the user. We’ve looked at creating a mailbox and modifying a user account. Let’s go back and delete the one we just created. So if we do a Remove-Mailbox, this will actually prompt you because you’re doing a destructive operation here.

021-remove-mailbox-add-modify-exchange-server-powershell

New-Mailbox creates an AD user and also mailbox enables that user.

The flipside of that is remove mailbox will actually remove the AD user and disconnect the mailbox. If you just want to delete the mailbox, you would do a Disable-Mailbox. So there’s this warning that hey do you really want to that? Because we’re going to delete the active directory user. I’m going ahead and type n for no.

If I really just want to get rid of the mailbox, disable mailbox, leave the AD user intact or if I just want to delete it and I don’t care about being prompted then you could use -Confirm:$false go ahead and completely remove that object and don’t even ask me about it.

022-remove-mailbox-add-modify-exchange-server-powershell

You can see that it comes back with no errors and if I run get-mailbox jasonh

023-remove-mailbox-add-modify-exchange-server-powershell

See I get an error because we actually deleted it and it no longer exists. Now the only other thing that we’ve looked at in the script is changing the settings on mailbox but of course you would do just a Set-Mailbox for that.

So you could do a Set-Mailbox $name (There are a bunch of different parameters out here. This command has about a hundred.)

There’s all kind of different things that you can change so one of the things I normally do is when I’m dealing with modifying recipients. If you’re not sure which commands are responsible for setting which parameters, go on to the properties object in the console.

024-properties-remove-mailbox-add-modify-exchange-server-powershell

For example Allan Miller and maybe I need to change a few things about this user such as we’ll add his middle initial.

025-properties-remove-mailbox-add-modify-exchange-server-powershell

We’ll change his job title to Sales Rep.

026-properties-remove-mailbox-add-modify-exchange-server-powershell

Next we’ll come over to Mailbox Features and we’ll disable MAPI connections for this user.

027-properties-remove-mailbox-add-modify-exchange-server-powershell

It’s kind of hard to see this but there’s a Exchange Management Shell Command button here down at the bottom.

028-properties-remove-mailbox-add-modify-exchange-server-powershell

If you look at that and this example we’re running set user, set CAS mailbox and maybe if we do one more thing like do a Send on Behalf for another user,

029-properties-remove-mailbox-add-modify-exchange-server-powershell

You can see that those three changes require a call to three separate cmdlets. One for Set-Mailbox.

030-properties-remove-mailbox-add-modify-exchange-server-powershell

One for Set-User and one for Set-CASMailbox. So you can use the gooey to cheat and figure out which commands you would need to kind of run in sequence here in your script. So that’s the process of going through adding, removing and modifying your recipients.

Videos You May Like

Agile Methodology in Project Management

0 57 0

In this video, you will gain an understanding of Agile and Scrum Master Certification terminologies and concepts to help you make better decisions in your Project Management capabilities. Whether you’re a developer looking to obtain an Agile or Scrum Master Certification, or you’re a Project Manager/Product Owner who is attempting to get your product or … Continue reading Agile Methodology in Project Management

Creating Users and Managing Passwords in Microsoft Office 365

0 98 1

In this Office 365 training video, instructor Spike Xavier demonstrates how to create users and manage passwords in Office 365.

Detailed Forensic Investigation of Malware Infections – April 21, 2015

2 191 1

In this IT Security training video, Security expert Mike Danseglio (CISSP / CEH) will perform several malware investigations including rootkits, botnets, viruses, and browser toolbars.

Write a Comment

Share your thoughts...

Please fill out the comment form below to post a reply.