PS to Office365

Windows PowerShell needs to be configured to run scripts, and by default, it isn't. You get the following error when you try to connect:

Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files.

To enable Windows PowerShell to run signed scripts, run the following command in an elevated Windows PowerShell window (a Windows PowerShell window you open by selecting Run as administrator):

Set-ExecutionPolicy RemoteSigned

You need to configure this setting only once on your computer, not every time you connect.

    1. On your local computer, open Windows PowerShell and run the following command.
              1. $UserCredential = Get-Credential
      1. In the Windows PowerShell Credential Request dialog box, type your Office 365 user name and password, and then click OK.
    2. Run the following command.
              1. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $UserCredential -Authentication Basic -AllowRedirection
      1. Note If you are an Office 365 operated by 21Vianet customer in China, use the following value for the ConnectionUri parameter:
    3. Run the following command.
              1. Import-PSSession $Session

4. Disconnect when done

Get-PSSession | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"} | Remove-PSSession



  • Get-Mailbox | Get-MailboxStatistics | Select-Object DisplayName, IsArchiveMailbox, ItemCount, TotalItemSize | Export-CSV –Path “C:\Logs\ExchangeOnlineUsage.csv” (output the usage statistics to a CSV)
  • Get-Mailbox -Filter {ArchiveStatus -Eq "None" -AND RecipientTypeDetails -eq "UserMailbox"} | Enable-Mailbox -Archive (Run the following command to enable the archive mailbox for all users in your organization (whose archive mailbox is currently not enabled)
  • Search-Mailbox -Identity "" -SearchQuery ' Subject:"Invoice" ' -DeleteContent (delete all emails with Invoice in Subject )
  • Search-Mailbox -Identity "" -SearchQuery {received:01/01/2018..01/01/2019} -DeleteContent (delete all emails inside date range (mm/dd/yyyy))
    • Search-Mailbox can search only 10 000 results, so you will need to repeat the command if you have more that 10 000 records.


Be sure to disconnect the remote PowerShell session when you're finished. If you close the Windows PowerShell window without disconnecting the session, you could use up all the remote PowerShell sessions available to you, and you'll need to wait for the sessions to expire. To disconnect the remote PowerShell session, run the following command.


Remove-PSSession $Session (Remove-PSSession 1) (1 is the Session id)

to find out the ID of a user, to manually configure Outlook:

to list Status of pst import:


to add O365 group to DistributionList ( possible only with PowerShell )

Add-DistributionGroupMember -Identity "Staff" -Member ""

Get-Mailbox -Identity user_name | select ExchangeGUID