Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


Log onto Office 365 upon Startup with Powershell

Post Author: Joe D365 |

We've talked about using PowerShell to help manage your Office 365 environment. And although you can connect to Office 365 using the following commands (below), why not use something easier?

Import-Module MSOnline
$O365Cred = Get-Credential
$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber
Connect-MsolService –Credential $O365Cred

There's a little thing called profiles. There are several locations where you can create profiles depending on your end goals.

  • This is for all users of the computer and for all shells: %windir%system32Windows­PowerShellv1.0profile.ps1
  • This is for all users of the computer, but only for the Microsoft.PowerShell shell: %windir%system32Windows­PowerShellv1.0Microsoft.Power­Shell_profile.ps1
  • This is for the current user only and all shells: %UserProfile%DocumentsWindows­PowerShellprofile.ps1
  • This is for the current user only and only for the Microsoft.PowerShell shell: %UserProfile%DocumentsWindowsPowerShellMicro­soft.PowerShell_profile.ps1

Remember these profiles aren't created by default. They exist only if you create them.

Using Your Profile %UserProfile%DocumentsWindowsPowerShellMicro­soft.PowerShell_profile.ps1 to connect to office 365, create the Directory 'WindowsPowershell' and create the file 'Micro­soft.PowerShell_profile.ps1':

Now, edit the file and add the lines of code mentioned above save the file:

Now every time you start up PowerShell, you will be prompted for your credentials and then automatically be logged into Office 365.

For more PowerShell tips and tricks, check out these resources:

Increase the CRM Activity Feed Follow Limit through PowerShell

Use PowerShell to Create Reports in Office 365

Managing Office 365 with PowerShell

Happy CRM'ing!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

One comment on “Log onto Office 365 upon Startup with Powershell”

  1. If you use something like:
    Get-Credential | Export-Clixml -Path
    just one time, you can avoid the credential prompt each time by changing
    $O365Cred = Get-Credential
    to
    $O365Cred = Import-Clixml -Path
    It's encrypted and very secure.

PowerObjects Recommends