Setting an Out of Office reply in Office365 with PowerShell.

Since I started my new role, something I have been tasked with getting to grips with more is PowerShell. From my limited experience with it so far I can already tell you I love it and it’s truly going to be massive going forwards, if not already.

So, I’ve started to wean myself into some tasks using the Shell whenever I can and this is one of the first I learned.

Sometimes, there’s a user that goes off sick or ‘leaves the business’ suddenly that you will need people contacting said user to be aware of. Sometimes it’s just everyone’s favourite “Thank you for your e-mail, we will endeavour to reply within 24 hours” in a shared mailbox. Well, to quote my boss “You can do that in PowerShell!”.

Firstly open your PowerShell Console or the Powershell ISE as an administrator (I prefer the ISE) and if you don’t know what the difference is then slip yourself along to this page: https://www.educba.com/powershell-vs-powershell-ise/ which seems to give a fairly decent run-down.

As I mentioned before you will need to open this as an admin; so make sure you right-click and run whichever platform you have decided to run as an administrative user.

Connecting to Office365

So firstly you need to connect to your O365 environment by Pasting this into the Script Window of your ISE (If you’re using the console then you’ll need to paste this into a text editor beforehand). The black lines are the actual script you need and the red is my explanation of what it actually does:

> Set-ExecutionPolicy RemoteSigned
This sets the script-running policy of your machine to allow scripts to be ran, but they need to be signed by a trusted publisher. The default is "Restricted" which means scripts will not run.

> $UserCredential = Get-Credential
Get-Credential will pop up a window asking for a username and password. The value of what you type into the credential window will the be stored into the $UserCredential variable for later use.

> $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection 
Here's the real meat of the operation. This will create a new PowerShell session to Office365 using the credentials contained within $UserCredential which we defined earlier. The whole lot will then be wrapped into a variable called $session.

> Import-PSSession $Session
This imports the remote session you have just created into your local session for modification.

Setting the Out of Office Message

So, that’s the first bit done. We’ve connected to our Office365 environment. Now we just need to set the Out of Office message which looks like a lot of code but it’s really not. Here it is:

> Set-MailboxAutoReplyConfiguration –identity “FirstName LastName” –InternalMessage “This is the message that will be sent internally" –ExternalMessage “This is the message that will be sent internally” –AutoreplyState enabled
This one is pretty self explanatory, the autoreply config is set for the mailbox with given name, with the messages defined for both internal and external users and enables the auto-reply. Note: the text in green are areas you will need to modify to meet your needs.

Then to end your session you need to run:

> Remove-PSSession $Session
This will end the connection to the O365 environment.

All done!

If you’re new to PowerShell and you’d like to get your head around it a bit more then you absolutely cannot go wrong by reading this: https://www.amazon.co.uk/Learn-Windows-PowerShell-Month-Lunches/dp/1617294160/ref=sr_1_1?dchild=1&keywords=Learn+Windows+PowerShell+in+a+Month+of+Lunches&qid=1589366044&s=books&sr=1-1

Richard

30-something Sysadmin from the Midlands, UK.