Logging Into Office 365 Powershell In Different Contexts

Recently I was trying to run some commands in office 365 powershell that involved get-distributionlist, set-distributionlist, get-mailbox, setmail-box, etc… commands. I found that these commands were not even registering as commands – it would throw back the error that says: “The term ‘get-mailbox’ is not recognized as the name of…”. I had logged in the normal way, using the method outlined in the bottom of this article:

http://www.office365forbiz.com/the-basics-of-office-365-powershell/

The term 'get-mailbox' is not recognized as the name of

Get-Mailbox failed

 

 

 

 

 

After a long and exasperating support call with Microsoft I came upon the answer – there is a second context to logging into powershell that will enable to the use of many commands. To be able to use the commands listed above, and many others (I don’t know the entire list), you must login as follows:

  1. Set-ExecutionPolicy RemoteSigned    (choose yes)
  2. Set-ExecutionPolicy Unrestricted        (choose yes)
  3. $Cred = Get-Credential
  4. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
  1. Import-PSSession $Session
  2. Connect-MsolService -Credential $Cred
  3. Import-Module Msonline            (This command is only Used in Window Powershell )

You will recognize step 6 as something you usually do to log in – if you do not run step 6, then there are other commands that will not work. Get-mailbox and all of the commands mentioned above will work, but commands like get-msoluser will fail.

The overall lesson here is that there are two different logon contexts here. If you are unsure which one you need, or you have commands failing to be recognized, then I recommend logging in with both contexts. If you already know all your commands will work in only one context, then are you safe to use just that one.

 

Leave a Reply

* Copy This Password *

* Type Or Paste Password Here *