Do more cool stuff with PowerShell and Office 365

Back in 2016, my colleague Microsoft MVP Vlad Catrinescu contributed a guest editorial to our popular newsletter WServerNews, which by the way now goes out each week to more than half a million IT professionals around the world! That issue of WServerNews was titled PowerShell and Office 365 and Vlad’s editorial was titled “Three cool things you can do in Office 365 with PowerShell” and it was highly popular with our readers, so I approached Vlad again recently to ask if he had any more “cool stuff” he could share with us to show what you can do with PowerShell and Office 365. Vlad’s reply was “Yes!” and he started off by explaining why:

In 2016 I had the chance to publish a small article in the WServerNews newsletter on three cool things you can do in PowerShell for Office 365. Two years in it’s now time to come back with three more cool things! But why is this important? Office 365 is now here, and most IT administrators need to update their skills to also know how to manage one or more services inside the cloud! In Office 365, some tasks that administrators must do on a frequent basis can get repetitive and waste precious time. Furthermore, multiple advanced settings are simply not available in the Office 365 Administration center. This is where PowerShell comes in. PowerShell is a command-line tool designed specifically for system administration. PowerShell helps IT professionals automate the administration of Windows Operating system as well as different applications such as SharePoint, Exchange, System Center and Office 365. PowerShell is becoming more powerful every day, and every job description you look at today lists PowerShell as a required skill. If you’re a systems admin and don’t know PowerShell yet, you need to start learning it!

In this present article, Vlad looks at three more cool things you can do with PowerShell and Office 365. But before we look at this let me briefly tell you a bit about Vlad. Vlad is a SharePoint and Office 365 consultant specializing in SharePoint and SharePoint Online deployments as well as hybrid scenarios. As a Pluralsight author, Microsoft Certified Trainer, and recognized international speaker, Vlad has helped hundreds of thousands of users and IT pros across the globe better understand and to get the most out of SharePoint. Vlad is also a Microsoft Most Valuable Professional (MVP) in SharePoint since 2013, has his own blog at, and he often shares his knowledge by speaking at local conferences and community events. Vlad also blogs at CMSWire as well as Computerworld and is often featured on other Microsoft sites such as Redmond Channel Partner. You can find Vlad’s website, and you can also connect with Vlad on LinkedIn and follow him on Twitter. Let’s now look at Vlad’s three tips for using PowerShell with Office 365. Note that he skips over the “how to connect to Office 365” basics (which you can find here) and just shows you the code that does the action for each scenario.

Cool stuff #1: Creating a secret Office 365 Group

PowerShell and Office 365Did you know that private groups in Office 365 are not private? If you create a private group via the user interface, everyone can still find it via the Discovery Tab in Outlook, as well as from Teams Search! Even worse, everyone can see the members inside! While some of the times this is not too bad, in some cases, like a reorg, we don’t want people to be able to find this at all unless they’re a member! Don’t trust me? Create a Private Office 365 Group with your admin account via the User Interface, and then go at a normal user’s computer and try to search for it — you will be surprised!

The only way to create an Office 365 Group that is truly secret is via PowerShell. You will first have to Connect to Exchange Online with PowerShell, which we will not cover here, and then at Group Creation, you need to specify the -HiddenGroupMembershipEnabled parameter. This will not hide it from search, but when someone gets to it, either by search or by a direct link, they will not be able to see the members inside, which is Step No. 1! Make sure you change the alias and email address to match your domain and policies.

-DisplayName “SecretGroup”
-Alias “O365Group-SecretGroup”
-EmailAddresses “[email protected]
-AccessType Private

It’s important that you specify this at creation since after creating the Office 365 Group, this property cannot be changed anymore. The second step is to hide the Office 365 Group from search, and this must be done as a second cmdlet. We will modify the group to set the HiddenFromAddressListsEnabled property to true. This will hide the private group from the Discovery Tab or Search, so users that are not members of that Group will not be able to find it anymore.

-Identity “O365Group-SecretGroup”
-HiddenFromAddressListsEnabled $true

Changing the HiddenFromAddressListsEnabled property can be done after group creation, so you can change this property for your existing Office 365 Groups as well.

Cool stuff #2: Create a report of external Office 365 Users in SharePoint Online

PowerShell and Office 365
One of the great advantages of Office 365 and SharePoint Online is how easy it is to collaborate with external users. You can easily invite external users, whether they are using Office 365 or not to your SharePoint sites and you can share documents, calendars, and list items without needing to go through IT. However, as the IT person, we need to stay informed of who’s sharing what in our tenant, and make sure we know what sites each external user has access to. Once you are connected to SharePoint Online, you can use the following PowerShell script that will output all your External Users, as well as what site they have access to, in a CSV File. You can then open this in Excel and filter per URL, per user and so on.

$SiteCollections = Get-SPOSite -Limit All
foreach ($site in $SiteCollections)
try {
for ($i=0;;$i+=50) {
$ExternalUsers += Get-SPOExternalUser -SiteUrl $site.Url -PageSize 50 -Position $i -ea Stop | Select DisplayName,EMail,AcceptedAs,WhenCreated,InvitedBy,@{Name = “Url” ; Expression = { $site.url } }
catch {

$ExternalUsers | Export-Csv -Path “C:\Scripts\ExternalUsersPerSC.csv” -NoTypeInformation

Cool stuff #3: Set an ‘out of office’ reply for a user

When users quit the company or take an unexpected leave of absence, we might want to set an out of office reply for that user. This way, if someone emails them, they will know who else they can contact for their query. For this example, we will need to be connected to Exchange Online. First thing I will do is a variable called $body, which will include the out of office reply, in HTML format.

$Body = @”
“Hello </br> </br>
Please Note I am not currently working for Globomantics anymore. </br> </br>
Please contact Vlad Catrinescu <a href=”mailto:[email protected]”>[email protected]</a> for any questions. </br> </br>
I will then use the Set-MailboxAutoReplyConfiguration PowerShell cmdlet to enable the Out of Office Replies for an account and set both the Internal and External message to the variable I have created earlier!
-Identity [email protected]
-ExternalMessage $body
-InternalMessage $body
-AutoReplyState Enabled

Learn more about PowerShell and Office 365

PowerShell in Office 365 doesn’t only allow IT professionals to automate boring and repetitive tasks, but it also enables the configuration of advanced properties that are not available in the User Interface. If you’re an IT professional and don’t know PowerShell yet, you need to learn it as it will be very useful with any Microsoft application that you work with.

If you are just starting with PowerShell and Office 365, there are multiple resources to help you get started. Here are a few:

For those of you who don’t know, Pluralsight is an online on-demand video training library offering more than 6,000 courses across different technologies. The PowerShell for Office 365 course covers all the basics and advanced topics of PowerShell for Office 365 and covers Users, Exchange, SharePoint, Skype for Business as well as the Compliance Center. If you have a subscription, you can watch it right away.

The Hey, Scripting Guy! Blog is a great resource for everything PowerShell. They don’t only cover PowerShell and Office 365, but everything related to PowerShell and they blog daily, so it’s a resource you should check out.

This book is your companion to administering PowerShell and Office 365. You will learn time-saving techniques such as how to streamline administrative tasks, and how to manage users, licenses, and Office 365 services. Expert and MVP Vlad Catrinescu introduces each chapter with an overview and fundamentals, such as how to connect to your required service in Office 365, so that you have a solid foundation for success. Benefit from learning the theory behind PowerShell and Office 365 and put your knowledge to practice with numerous hands-on code examples.

About The Author

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top