Using PowerShell for Microsoft 365

PowerShell is a powerful tool that can be used to manage and automate tasks in Microsoft Office 365. Here’s how to connect to Microsoft Office 365 using PowerShell:

  1. Open PowerShell as an administrator. To do this, right-click on the PowerShell icon and select “Run as administrator.”
  2. Install the Microsoft Online Services Sign-In Assistant and the Azure Active Directory PowerShell for Graph modules. You can do this by running the following command:
    Install-Module -Name MSOnline
    Install-Module -Name AzureAD
  3. Connect to Microsoft Office 365 using PowerShell by running the following command:
    Connect-MsolService
    This command will prompt you to enter your Office 365 administrator credentials.
  4. If you have multi-factor authentication enabled, you will need to use a different command to connect to Office 365. Run the following command:
    Connect-AzureAD -AccountId <YourUPN>
    Replace <YourUPN> with your Office 365 user principal name.
  5. Once you have successfully connected to Office 365 using PowerShell, you can use a wide range of cmdlets to manage your Office 365 environment.

Here are a few examples of PowerShell cmdlets that you can use:

  • Get-MsolUser – Returns information about Office 365 users.
  • Set-MsolUser – Updates properties for Office 365 users.
  • Get-MsolGroup – Returns information about Office 365 groups.
  • Set-MsolGroup – Updates properties for Office 365 groups.
  • Get-Mailbox – Returns information about Exchange Online mailboxes.
  • Set-Mailbox – Updates properties for Exchange Online mailboxes.

Here are some examples of how you can use PowerShell with Office 365:

  1. Creating and managing users

You can use PowerShell to create and manage Office 365 user accounts. For example, to create a new user account, you can use the following cmdlet:

New-MsolUser -UserPrincipalName <UPN> -DisplayName <DisplayName> -FirstName <FirstName> -LastName <LastName> -Password <Password>

This will create a new user account with the specified user principal name, display name, first name, last name, and password.

To get a list of all Office 365 users, you can use the following cmdlet:

Get-MsolUser

You can also update user properties using PowerShell. For example, to update a user’s password, you can use the following cmdlet:

Set-MsolUserPassword -UserPrincipalName <UPN> -NewPassword <NewPassword> -ForceChangePassword $true
  1. Managing Exchange Online mailboxes

You can use PowerShell to manage Exchange Online mailboxes in Office 365. For example, to get a list of all mailboxes in Exchange Online, you can use the following cmdlet:

Get-Mailbox

You can also create new mailboxes using PowerShell. For example, to create a new mailbox for a user, you can use the following cmdlet:

New-Mailbox -UserPrincipalName <UPN> -Name <DisplayName> -Alias <Alias> -Password (ConvertTo-SecureString -String "<Password>" -AsPlainText -Force)

This will create a new mailbox with the specified user principal name, display name, alias, and password.

You can also update mailbox properties using PowerShell. For example, to set a mailbox size limit, you can use the following cmdlet:

Set-Mailbox -Identity <UPN> -ProhibitSendReceiveQuota <SizeLimit> -ProhibitSendQuota <SizeLimit> -IssueWarningQuota <SizeLimit>
  1. Managing SharePoint Online sites

You can use PowerShell to manage SharePoint Online sites in Office 365. For example, to get a list of all SharePoint Online sites in your organization, you can use the following cmdlet:

Get-SPOSite

You can also create new SharePoint Online sites using PowerShell. For example, to create a new team site, you can use the following cmdlet:

New-SPOSite -Title <Title> -Url <Url> -Owner <Owner> -StorageQuota <StorageQuota> -Template "STS#0"

This will create a new team site with the specified title, URL, owner, storage quota, and template.

You can also update SharePoint Online site properties using PowerShell. For example, to set the storage quota for a site, you can use the following cmdlet:

Set-SPOSite -Identity <Url> -StorageQuota <StorageQuota>

These are just a few examples of how you can use PowerShell with Office 365. By using PowerShell to manage your Office 365 environment, you can save time and automate routine tasks, making it easier to manage your organization’s IT infrastructure.

Related Post