Use PowerShell to Manage Office 365

Doctor Scripto

Summary: BATCHman battles The Consultant and uses Windows PowerShell to manage Office 365 easily.   Microsoft Scripting Guy Ed Wilson here. Sean Kearney brings us Episode 8 of the BATCHman series today.   Whenever trouble happens in systems and people will call, And darkness rolls out causing your fall, Creatures of bits roam in the night, Shine to the sky, the bright bluish Light, And call to…BATCHman ! …and, oh yes, his sidekick Boy Blunder Cmdlet, too.   In Today’s Episode, BATCHman Encounters the Evil of The Consultant Oh, the times are dangerous and frightening. Somewhere in Redmond, a team of IT people is running about madly with their hands in the air. “AIAHGIHAIGHIH!!! AIHAGHAHGHAGHIHI!!! We can’t manage anything! The sky is falling the sky is falling! Aihihighighighhghgi!!!!” Panic (and apparently Henny Penny) has gripped them by the ears. The systems administrator is on the phone madly dialing BATCHman directly. “BATCHman!!! BATCHman!!! You’ve got to help us!!! HURRY!!! AIGHAIHGHAHGHAH!!!!!” “Certainly, why we’ll be happy to b…” He is cut off by more screaming and the sound of breaking glass. “HURRY!!! HURRY!!! It’s all over!!! AIAGIHGAIH” *click* The phone is dead. Cmdlet without blinking yells, “To the WinMobile!” Cmdlet takes off to the scene in a flash. BATCHman is left behind to wonder how he should get there. Our hero, looking about scratching his head, finds the only alternate means of transport. “Well, err,. to the BATCHunicycle!” as he deftly hops on and wobbles his way to the crime scene. ***A good while later*** BATCHman arrives. He sees Cmdlet. “For future reference, the sidekick does not drive the superhero car,” tapping Cmdlet on the nose. “You’ll take the BATCHunicycle home,” he grinned. Cmdlet has in the meantime gathers the details. “Pretty evil situation. The IT staff came in over the weekend The Consultant migrated them to Office 365.” BATCHman is puzzled. That doesn’t seem evil. That seems quite nice actually. “I don’t think you’re getting it. Not *A* Consultant but *THE* Consultant. He walked in, migrated them, left a bill on the table and didn’t tell anybody his plans. They have no instructions or documentation.” BATCHman suddenly realizes the issue. One of the nastiest, grungiest creatures on the planet simply called “The Consultant” had arrived on the scene. His claim to the evil world would be to improve infrastructure, bill clients, and then leave them with no documentation of what happened. Often doing it as a surprise launch on a weekend, his calling card was nasty: a hefty bill and a panicked IT staff on Monday morning. However, today The Consultant has met his match. He outdid himself. He used Office 365, and BATCHman and Cmdlet were on the scene. “Never fear, bring the IT staff in here. Let them know BATCHman is on the scene. We can solve all their woes with Windows PowerShell!” Moments later, the mob of IT staff and one very frightened cat gather in the boardroom. BATCHman looks at them. “Was there anything left behind? Other than the bill?” Senior Systems Administrator Sue walks over. “Just this envelope with an ID/password and the words ‘Office 365’. Everything is running, but none of our existing systems seems to work for management. We need to create some new users as well as manipulate our existing distribution lists.” BATCHman calmly strokes his chin. “Office 365, you say? Well, then, as long as you have credentials, we’re okay. For basic access, all we need to do is log on to with the provided credentials, and we can do everything from there. But never fear. We can use Windows PowerShell as well to manage the email.” BATCHman quickly logs on with the provided user ID and password to the Web-based interface for, showing them a basic management interface. “Now, normally to create a user in Exchange, we might use the New-Mailbox or Enable-Mailbox cmdlets. But the process for Office 365 isn’t too difficult. The workflow is almost identical to Exchange because it’s just Exchange in a hosted environment. First, we add the needed modules. The first is the MSONLINE module, which we download from the Office 365 website.”

IMPORT-MODULE MSONLINE “Next we provide the credentials to Windows PowerShell using Get-Credential and connect to the Office 365 web services. The UserPrincipalName or email address and password The Consultant left you are your credentials.”

CONNECT-MSOLService -credential $Cred “After this is done, we bring down the Exchange cmdlets using implicit remoting from the Office 365 server.”

$s = NEW-PSSESSION -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $Cred -Authentication Basic -AllowRedirection
$importresults=import-pssession $s The IT team begins to breathe more clearly. They are not locked out and this is scriptable. Windows PowerShell and BATCHman have once again saved the day. “After the cmdlets are installed, you’ll find that many of the abilities to add and remove distribution groups and memberships are identical to what you’re used to because it is just Exchange on the back end. To create a user, however, is slightly different. We must use the following cmdlet.”

NEW-MSOLUSER “So all you’ll need to do to create users is use the previously cached credentials you stored in the $CRED variable and run the cmdlet like so. This is presuming an email address of, the first name of ‘Evil,’ and the last name of ‘Consultant.’”

NEW-MSOLUSER –userprincipalname ‘ -DisplayName ‘Evil Consultant’ –Firstname ‘Evil’ –Lastname ‘Consultant’ “But BATCHman! Creating users is fine, but what do we do to delete them?! Usually that is coming down the line quicker than creating a user! We need to ensure that when our payroll application disables the staff member, it can still run our existing Windows PowerShell module!” BATCHman smiled. “I concur that’s just as important, but never fear, Windows PowerShell is here!” They pause as BATCHman quickly types.

REMOVE-MSOLUSER –userprincipalname ‘’ “There is also a web-based interface at to allow a richer interface to manage the accounts in the system. You’ll also find a plethora of online resources to aid in managing the system.” The IT team breathes a sigh of relief. Although The Consultant changed their back end on them, automation is still here to aid them. Being in the cloud did not mean being left alone on the ground, thanks to Windows PowerShell. “By the way, if you have the card of that particular villain, I’d like to pass it along to the Better Business Bureau.”   Thanks Sean for this episode of BATCHman. Join us tomorrow for another exciting episode of BATCHman—Episode 9! I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy    


Discussion is closed.

Feedback usabilla icon