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:
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:
- Set-ExecutionPolicy RemoteSigned (choose yes)
- Set-ExecutionPolicy Unrestricted (choose yes)
- $Cred = Get-Credential
- $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
- Import-PSSession $Session
- Connect-MsolService -Credential $Cred
- 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.