Connect Office 365 with Windows PowerShell

Add permission

To run scripts in Windows PowerShell, we need to run the following command as an administrator. To do this we start the PowerShell as administrator (right mouse button and choose to run as administrator). This command only needs to be executed once.

PS C:\>

Set-ExecutionPolicy RemoteSigned

Establish connection

Now start a normal PowerShell without administrator privileges and execute the following commands:

Login with your Office 365 credentials

PS C:\>

$UserCredential = Get-Credential

To start the session execute:

PS C:\>

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

PS C:\>

Import-PSSession $Session

Check connection

To check if you are connected, you can print some mailbox information with:

PS C:\>

Get-Mailbox

Close connection

If you are done, close the session with:

PS C:\>

Remove-PSSession $Session

References

Windows Docs - Connect to Exchange Online PowerShell

https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/connect-to-exchange-online-powershell?view=exchange-ps

Windows Docs - Get-Mailbox

https://docs.microsoft.com/en-us/powershell/module/exchange/mailboxes/Get-Mailbox?view=exchange-ps

Kommentare

Was ist die Summe aus 4 und 5?