Another 10 useful PowerShell cmdlets for Exchange

After all the positive feedback to my article “10 Useful PowerShell cmdlets for Exchange” (thanks again to Steve Goodman), I have decided to write a second part of it with another 10 useful PowerShell commands for Exchange and Office 365.

Same procedure as the last time. Some of you may already know the commands, but for some of you they are maybe new…

In any case, I wish you fun with reading this article…

 

This time, I will start with a command for Office365 and Exchange…

 


1 GET-MIGRATIONUSER

 

Migrating mailboxes from one Exchange to another, or even to Exchange Online is no rocket science anymore. How to do that, we can find in many articles in the Tech Blogs all over the world. I want to write here about how to check the status of the migration. All we need to do is to connect by PowerShell to the right endpoint. In this example, I am connecting to the Exchange Online by using the following command:

 

After I am connected to my endpoint, I can check the status of the migration batch with the following command:

As we can see in the screen shot, we get the status of any move request within our migration batch.

By knowing this command and understanding PowerShell, we are free to modify this command as well. Here´s another example:

In this example, only those mailboxes will be shown, which don’t have the status “completed”. This is very helpful to keep an easy overview of huge migration batches .


 

2 GET-MESSAGETRACKINGLOG

By working as an Exchange administrator or in 2nd level support, one of the main tasks is to track messages. Microsoft has created the Exchange Toolbox for this.

If we start it, we can see a collection of tools, which help us to investigate some issues.

However, to check the Tracking Log there is a faster and easier way to do it.

We simply can use the following PowerShell command for that:

In my example, I was searching for all mails sent during a time range on a specific exchange server, which have been sent to a specific recipient:

However, there are also some other options for searching. Instead of searching by recipient, we are also able to search by:

  • MessageSubject
  • Reference
  • Sender
  • InternalMessageId
  • MessageId

 

In the most cases we search for sender, recipient or message subject. You can see the output in the example above, with which we can work…

Another option is to create a Grid-View like I described in my other article about PowerShell commands.

Now, I want to show you a different option for creating nice reports:

If we use the following command:

We will get a report in form of an HTML file, which looks like the one below.


 

3 SET-CALENDERPROCESSING

The next cmdlet works on Exchange server 2016 an on Exchange online, we can use the cmdlet to modify calendar processing options for resource mailboxes, which include the calendar attendant, resource booking assistant, and the calendar configuration. With this cmdlet we are able to do many things, let’s go for some examples…

There is a company which has, let’s say, 10 meeting rooms. 9 of them are free to book for all employees, but one is only available for a dedicated group in the company, e.g the HR.

So now we need to prevent all users except the HR to be able to book this meeting room.

To do this, we simply run the following command:

Now we are sure, that only these users are able to make bookings for the specified meeting room.

Another example automates the processing of calendar requests to the resource mailbox SBB23:

With the Set-CalendarProcessing cmdlet we are also able to disable the automatic processing. Here´s an example for a company car with the license plate GR123321:


 

4 SET-MAILBOXAUTOREPLYCONFIGURATION

Ready for number 4? This cmdlet, is again one for all the Exchange admins in the field. Sometimes it happens that users go to vacation and they forget to set the Out of Office Notification. Another example is, when a user is sick for a while and there is also a need to set the Out of Office notification.

There are different ways how to do that, but by some easy ways it could be a problem with the legal. To be fast to make it and to stay on the legal side, I prefer to user PowerShell for this task.

In my example the user Desmond Miles went to vacation and he forgot to activate his Out of Office notification. So, to be sure that people who are writing him will receive the right notification, I use the following command:

We can also schedule the time period from when till when we want to have the notification activated. Here, the way how this works:


 

5 NEW-MAILBOXEXPORTREQUEST

Here we go, halftime with the number 5…

The New-MailboxExportRequest cmdlet helps us to create a PST File from a user mailbox without the compulsion to login to the users account. This helps us, like the previous cmdlet, not to get in trouble with the legal. This command only works with on-premise Exchange servers, if you need to make a PST export from an Exchange Online mailbox, you can check my other article here.

Note: This cmdlet is available only for the Mailbox Import Export role and by default, that role isn’t assigned to a role group. To use this cmdlet, you need to add the Mailbox Import Export role to a role group (for example, to the Organization Management role group). Another requirement for exporting to a file share ist to grant the group ” Exchange Trusted Subsystem” read/write permissions to the target share.

In the first example we are going to create a PST export of the user mailbox “User01” to a file share server. To do that, we can run the following command:

How about an archive? The command is almost the same one! We only need to add the option “IsArchive”

Sometimes we don’t want to export the whole mailbox to a PST file. Imagine, we are just interested in messages with the words “company” and “phone”, which have been received before a specific date. For this, we can edit our command in the following way:

This commands can help us with the preservation of evidence, for example…

At this point, I would like to point out the following: with great power comes great responsibility!!
This is a very powerful command. Be aware of it…


 

6 GET-PARTNERAPPLICATION

This command works for Exchange on-premise and for Exchange Online. With this command we are able to configure partner applications such as Microsoft SharePoint to access the resources of our Exchange environment.

If I want to get access to data in a system, I usually have to log in (authenticate) and I must have obtained the required permissions (authorization). For users, this is a well-known process that you need to log in to the Active Directory first and then log on to the SID and their group memberships, for example, to access file shares that have ACL’s associated permissions added to them. For the Exchange administration with RBAC, users are also used, but then they are granted permissions via roles.

 

Microsoft describes this function like:

When Exchange 2013 receives an access request from a partner application via Exchange Web Services (EWS), it parses the www-authenticateheader of the https request, which contains the access token signed by the calling server using its private key. The auth module validates the access token using the partner application configuration. It then grants access to resources based on the RBAC permissions granted to the application. If the access token is on behalf of a user, the RBAC permissions granted to the user are checked.

So, here´s an example how this command works for us:

If we use the following command:

We are refreshing the auth metadata for the HRApp partner application.

 

Another example is, when we want to create a new HRApp partner application called HRApp:

The ApplicationIdentifier parameter specifies a unique application identifier for the partner application that uses an authorization server. When specifying a value for the ApplicationIdentifier parameter, you must also use the UseAuthServer parameter.

 


 

7 NEW-ITEM

Now let’s talk about one we all know (I guess). Sometimes when I am writing a script I want to create a dedicated directory to, for example, dump out files or reports. When I write scripts alone or together with Dominic, I try to make them as universal as possible. Many things can be handled with variables, but sometimes we have to create things, in this case a directory.

To create a directory with PowerShell we simply need one command: New-Item.

 

With the New-Item cmdlet we can create different things like directories, files, registry keys, etc.

Here are some examples how to use it:

If we want to create a new directory on the C:\ partition for our PowerShell scripts, we can go with this command:

If we want to create a file in this directory, we can use almost the same command, for creating a .txt file we will use:

How about existing files? If we try to create a new file but this file already exists, we will receive the following error message:

However, if we want to overwrite the existing File, we have the option –Force to overwrite the default behavior. Then the command will look like this:

 


 

8 NEW-MSOLUSER

Let’s talk abit about the Office 365 cloud now. The next command is more known by using its bro Get-MsolUser.

I want to show you now, how to create new users in the Azure Active Directory. After we have made a successful login to the Azure AD throughPowerShell and successfully downloaded the right PowerShell module before, we can use the command New-MsolUser to create new users.

Now,I want to show you some examples, how we can do that…

If we need to create one new user, we can use the following command:

This is easy going, but to create a bulk of new user accounts there must be a easyier way… Yes, there is one!

 

First we need to create a CSV file. This can look like this:

 

After we have created this file, we can run the following command:

Now, PowerShell is doing magic things, and all users from our CSV file will be created.

🙂

 

With the New-MsolUser cmdlet we can also create new users and assign a license straight away:

 

There are also a bulk of optional parameters:


9 GET-HOTFIX

So, the second to last for this article. The Get-Hotfix cmdlet gets hotfixes (also called updates) that have been installed on either the local computer (or on specified remote computers) by Windows Update, Microsoft Update, or Windows Server Update Services; the cmdlet also gets hotfixes or updates that have been installed manually by users.

In the first example we go to check which updates were already installed on my client computer. To do that I simply run the following command:

 

And as we see in the print screen, there are just a few updates installed on my machine:

In the 2nd example I want to check which security hotfixes are installed on a list of computers. To do that we can use the following command:

 

As we see, there are a couple of patches installed:


 

10 NEW-MANAGEMENTROLEASSIGNMENT

The (last) command for todays article is working for on-premise Exchange servers and for Exchange Online as well. With the New-ManagementRoleAssignment cmdlet we are able to manage roles for department groups. Maybe it sounds strange, but when I show you some examples, all will be clearer…

In the first example, I have to assign the Mail Recipient role to the 2nd level helpdesk of our company. To do that I run the following command:

 

We are also able to get some information about management roles. This example assigns the MyVoiceMail role to the “Sales end-users” role assignment policy. First, the IsEndUserRole property on the MyVoiceMail role is verified to be sure it’s set to $true, indicating it’s an end-user role:

 

After the role has been verified to be an end-user role, the role is assigned to the “Sales end-users” role assignment policy.


 

BONUS

While preparing this article, I came across another command, which I do not want to withhold from you. This command can be used in many ways, I want you to show a funny example for it.

Since PowerShell 3.0 we can use the Invoke-RestMethod. That means we are able to put curl in our PowerShell. Here, my example what we can do with it:

 

 

I am located in Zurich Switzerland, so for me, the weather here is important. When I run the command above, it will show me following result in my PowerShell console:


Go and play around with this command a little bit. An overview of available options can be found here: http://wttr.in/:help

 

I hope you enjoyed this article about another 10 PowerShell commands.

I hope you also have a sunny day like we here, as you can see in that last screen shot… But, hopefully just a bit warmer.


Leave a Reply

Your email address will not be published. Required fields are marked *