Automate connecting to Office 365 with PowerShell

[vc_row][vc_column][vc_column_text]Connecting to Office 365 with PowerShell enables you to perform many functions not available in the GUI.  But what if you want to automate these functions.  Connecting to Office 365 with PowerShell will prompt you for credentials for that session.  Now credentials can be saved in the script but that would be a security issue having your password saved in a file.  The way around that would be to save your password as a secure string in a text file.  And that is easy:

Read-Host -Prompt “Enter your password” -AsSecureString | ConvertFrom-SecureString | Out-File “C:\scripts\cred.txt

Your password will be encrypted and look like this:

$AdminName = “admin@domain.onmicrosoft.com”
$Pass = Get-Content “c:\scripts\cred.txt” | ConvertTo-SecureString
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
$Session = New-PSSession`
-ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/`
-Credential $cred -Authentication Basic -AllowRedirection

Import-PSSession $Session

After the connection part you can add your desired function to the script.  Let’s say for instance you want to export a list of shared mailboxes.

Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited | Select DisplayName | sort DisplayName | Export-Csv c:\scripts\SharedMailboxes.csv -NoTypeInformation

**This illustrates how an O365 administrator can introduce automation in scripts that they own and control individually. For scheduling automation tasks it is recommended to use a dedicated service account.[/vc_column_text][/vc_column][/vc_row]

Share:

Facebook
Twitter
LinkedIn

Contact Us

On Key

More Posts

Mastering Azure AD Connect - A Comprehensive Guide by WME
Active Directory

Mastering Azure AD Connect – A Comprehensive Guide

Modern businesses are fast moving toward cloud-based infrastructure. In fact, cloud-based business is not just a trend anymore but a strategic necessity. Microsoft’s Azure Active Directory (Azure AD) has become a frontrunner in this domain. It

Read More »
Security Best Practices in SharePoint
Office 365

Security Best Practices in SharePoint

Microsoft SharePoint is an online collaboration platform that integrates with Microsoft Office. You can use it to store, organize, share, and access information online. SharePoint enables collaboration and content management and ultimately allows your teams to

Read More »
The Ultimate Guide to Microsoft Intune - Article by WME
Active Directory

The Ultimate Guide to Microsoft Intune

The corporate world is evolving fast. And with that, mobile devices are spreading everywhere. As we venture into the year 2024, they have already claimed a substantial 55% share of the total corporate device ecosystem. You

Read More »
Protecting Microsoft 365 from on-Premises Attacks
Cloud Security

How to Protect Microsoft 365 from On-Premises Attacks?

Microsoft 365 is diverse enough to enrich the capabilities of many types of private businesses. It complements users, applications, networks, devices, and whatnot. However, Microsoft 365 cybersecurity is often compromised and there are countless ways that

Read More »
Be assured of everything

Get WME Services

Stay ahead of the competition with our Professional IT offerings.