Home » Top 10 Common Office 365 PowerShell Commands List | Most Useful

Top 10 Common Office 365 PowerShell Commands List | Most Useful

by admin
Top 10 Common Office 365 PowerShell Commands

 

Office 365 PowerShell command helps a user to manage Office 365 efficiently. With O365 admin center, a user can not only manage Office 365 user accounts and licenses but also various server products like Exchange, SharePoint Online, etc. This commands enables a user to manage information across different server products and performs effectively for bulk operations. Therefore in this post, we will describe the ten most useful PowerShell commands that will enable to ease the work in managing Office 365 groups in an organization. So read on and explore this article to the fullest.

1. Link Office 365 Instance with PowerShell

Initially, install Office 365 for Windows PowerShell and connect with Office 365 instance. Following are the steps to implement the same,

    • Go this link https://www.microsoft.com/en-us/download/details.aspx?id=41950 and download MS Online Services Sign-in Assistant for IT Professionals RTW
    • Now, import the PowerShell’s Online services module for MS AAD(Azure Active Directory) and Office 365,

Install-Module -Name AzureAD
Install-Module -Name MSOnline

    • Then, provide the Office 365 admin credentials by executing the following command,

$Cred = Get-Credential

  • Now, generate a remote PowerShell session with the following command,

$O365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic -AllowRedirection

    • Then, in the local Windows PowerShell session, the session commands needs to be imported,

Import-PSSession $O365

    • Once it is imported, connect it all the Office 365 services,

Connect-MsolService –Credential $O365

After the modules are imported for Windows PowerShell, now you can seamlessly manage Office 365 instance.

2. Connect Exchange Online and SharePoint Online with PowerShell

A user can connect MS Exchange Online and SharePoint individually. Moreover, connecting Exchange Online via PowerShell is almost the same while connecting to O365.

Following are the commands to be implemented,

$Cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic –AllowRedirection

On the other hand, linking the SharePoint Online is quite different. You need to first download and install SharePoint Online Management Shell. After which, you can execute the following PowerShell script,

$admin=”Admin@enterprise.onmicrosoft.com”
$orgname=”enterprise”
$userCred = Get-Credential -UserName $admin -Message “Type the password.”
Connect-SPOService -Url https://$orgname-admin.sharepoint.com -Credential $userCred

3. Find the Available List of O365 PowerShell commands

Now, to fetch the available Office 365 PowerShell cmdlets, implement the following Get-Command,

Get-Command -module MSOnline

You can also get the list of available commands for Azure Active Directory using the below cmdlet,

Get-Command -module AzureAD

4. Get All Office 365 Users List via PowerShell Script

In order to get the list of Office 365 users along with the licenses, a user must make use of Get-MsolUser command. It will not only retrieve the users with the license but also enlist the DisplayName, City, Department and ObjectID elements.

Get-MsolUser | Select DisplayName, City, Department, ObjectID

    • To check the number of account licenses, following is the script to be executed,

Get-MsolAccountSku

    • To obtain all the available services, you need to run the below script,

Get-MsolAccountSku | select -ExpandProperty ServiceStatus

5. Generate a New User in O365 Via PowerShell

In this, you need to create a new user in Office 365. For that, use New-MsolUser command.

New-MsolUser -UserPrincipalName kStanley@enterprise.onmicrosoft.com -DisplayName “Kenneth Stanley” -FirstName “Kenneth” -LastName “Stanley”

6. Eliminate a User from All Sites via PowerShell

So as to remove a user at once from all sites, make use of the below command,

Get-SPOSite | ForEach {Remove-SPOUser -Site $_.Url -LoginName ” kStanley@enterprise.onmicrosoft.com”}

7. Change Office 365 Password with PowerShell

For a user who needs to change the password in Office 365, they can run Set-MsolUserPassword command. A user can either mention the new password as listed in the below commands else can also discard the NewPassword variable so that the system will auto-generate a random password.

Set-MsolUserPassword -UserPrincipalName
kStanley@netwrixqcspa.onmicrosoft.com -NewPassword P@SSw0rd!

8. Manage Office 365 Groups Via PowerShell commands

One can manage group membership in Office 365 using PowerShell scripts. In order to restore the list of all Office 365 groups, run Get-MsolGroup command. Also, to add users to a group, run Add-MsolGroupMember command and to remove a user from the group, execute Remove-MsoGroupMember cmdlet.

Add-MsolGroupMember -GroupObjectId 5b61d9e1-a13f-4a2d-b5ba-773cebc08eec -GroupMemberObjectId a56cae92-a8b9-4fd0-acfc-6773a5c1c767 -GroupMembertype user

Here, GroupObjectId: It is the groups hexadecimal ID

GroupMemberObejctId: It is the user object ID, which you will get by running the below command,

Get-MsolUser | Select ObjectID.

9. Create a PowerShell Site Collection Using PowerShell

Below is the command to create a site collection in SharePoint by running the following PowerShell script,

New-SPOSite -Url “https://enterprise.sharepoint.com/sites/NewSite” -Owner “kStanley@enterprise.onmicrosoft.com” -StorageQuota “100” -Title “New Site”

10. Generate Reports in Office 365 Via PowerShell

As you are aware of the fact that PowerShell is the best utility for creating different reports. Following are some useful Office 365 reports that can be generated via PowerShell

    • If you want to fetch details about all mailboxes, run the below cmdlet,

Get-mailbox | get-MailboxStatistics

    • To find all the mailboxes that haven’t logged in since last 30 days, following is the cmdlet to implement the same,

Get-Mailbox –RecipientType ‘UserMailbox’ | Get-MailboxStatistics | Sort- Object LastLogonTime | Where {$_.LastLogonTime –lt ([DateTime]::Now).AddDays(-30) } | Format-Table DisplayName, LastLogonTime

    • To generate a report showing the details of the highest volume of senders and recipients, execute the below command,

Get-MailTrafficTopReport

    • To get the report showing all groups and their members, run the below command,

function Get-AllO365Members
{
Try
{
$O365Groups=Get-UnifiedGroup
foreach ($O365Group in $O365Groups)
{
Write-Host “Group Membership: ” $O365Group.DisplayName -ForegroundColor Green Get-UnifiedGroupLinks –Identity $O365Group.Identity –LinkType Members Write-Host
}
}
catch [System.Exception]
{
Write-Host -ForegroundColor Red $_.Exception.ToString() }
}
Get-AllO365Members

Final Verdict

By reading this post, one can smoothly manage Office 365 using PowerShell commands that provides a friendly Office 365 environment. Henceforth, we have discussed the ten most useful Office 365 Powershell commands that will moreover help a system administrator to execute various operations instantly.

Related Posts

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More