Connect Powershell to Exchange Online
1. Connecting to an Office 365 instance with PowerShell
3. Enter your Office 365 admin credentials:
$Cred = Get-Credential
4. Create a remote PowerShell session:
$O365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic -AllowRedirection
5. Import the session commands into the local Windows PowerShell session:
Import-PSSession $O365
6. Connect to all Office 365 services:
Connect-MsolService –Credential $O365
2. Connecting to Exchange Online and SharePoint Online with PowerShell
$Cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic –AllowRedirection
Getting a list of available Office 365 PowerShell cmdlets
To get a list of all available Office 365 PowerShell commands, we need to run the Get-Command cmdlet:
Get-Command -module MSOnline
4. Getting a list of all Office 365 users with PowerShell
If you need to provide a list of Office 365 users and licenses, use the Get-MsolUser cmdlet. It’ll retrieve all users with a valid license in the Office 365 tenant, along with the DisplayName, City, Department and ObjectID parameters.
Get-MsolUser | Select DisplayName, City, Department, ObjectID
To see the number of account licenses, you need to run the following cmdlet:
5. Creating a new user in Office 365 with PowerShell
New-MsolUser -UserPrincipalName JSmith@enterprise.onmicrosoft.com -DisplayName "John Smith" -FirstName “John” -LastName “Smith”
To create a new user, we use the New-MsolUser command:
New-MsolUser -UserPrincipalName JSmith@enterprise.onmicrosoft.com -DisplayName "John Smith" -FirstName “John” -LastName “Smith”
The system will output the user’s password and license status data.
6. Removing a user from all sites with PowerShell
Get-SPOSite | ForEach {Remove-SPOUser -Site $_.Url -LoginName " JSmith@enterprise.onmicrosoft.com"}
Get-SPOSite | ForEach {Remove-SPOUser -Site $_.Url -LoginName " JSmith@enterprise.onmicrosoft.com"}
7. Changing a password in Office 365 with PowerShell
Set-MsolUserPassword -UserPrincipalName JSmith@Netwrixqcspa.onmicrosoft.com -NewPassword P@SSw0rd!
8. Managing group membership in Office 365 with PowerShell
Add-MsolGroupMember -GroupObjectId 5b61d9e1-a13f-4a2d-b5ba-773cebc08eec -GroupMemberObjectId a56cae92-a8b9-4fd0-acfc-6773a5c1c767 -GroupMembertype user
GroupObjectId is the hexadecimal ID of the group, which you can get from the Get-MsolGroup command. GroupMemberObejctId is the user object ID, which you can find by running this command:
Get-MsolUser | Select ObjectID.
To remove a user from a group, use the Remove-MsoGroupMember cmdlet.
9. Creating a SharePoint site collection with PowerShell
We can also create a SharePoint site collection using PowerShell:
New-SPOSite -Url "https://enterprise.sharepoint.com/sites/NewSite" -Owner "JSmith@enterprise.onmicrosoft.com" -StorageQuota "100" -Title "New Site"
10. Creating reports in Office 365 with PowerShell
PowerShell is a great tool for making different reports. Here are some useful Office 365 reports done via PowerShell:
Details about all mailboxes:
Get-mailbox | get-MailboxStatistics
A list of all mailboxes that haven’t been logged into during the last 30 days:
Get-Mailbox –RecipientType 'UserMailbox' | Get-MailboxStatistics | Sort-Object LastLogonTime | Where {$_.LastLogonTime –lt
([DateTime]::Now).AddDays(-30) } | Format-Table DisplayName, LastLogonTime
A report on the highest volume senders and recipients:
Get-MailTrafficTopReport
A report on all groups and their members:
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
Note that most of the reporting cmdlets were deprecated in January 2018 and replaced by the new MS Graph Reporting API. Therefore, some reports are now available only in the Office 365 Security & Compliance Center.


0 Comments