Powershell script to connect to office365 with username and password

Here is the PowerShell script to connect to office365 with username and password and run queries.

$Office365Username = "info@spiderip.com"
$Office365Password  = ConvertTo-SecureString ‘spd$7r’ -AsPlainText -Force


 Get-PSSession | Remove-PSSession 

 $Office365Credentials  = New-Object System.Management.Automation.PSCredential $Office365Username, $Office365Password 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Office365credentials -Authentication Basic –AllowRedirection -ErrorAction Stop        
 Import-PSSession $Session -AllowClobber | Out-Null 
 Connect-MsolService -Credential  $Office365Credentials

Once connected You can run the queries. the below query will list the office365 shared mailbox details

Get-Mailbox -RecipientTypeDetails SharedMailbox | select Alias

Disconnect the session using the following command

Get-PSSession | Remove-PSSession

Comments