PowerShell script that will launch & sign onto Office 365 admin console

In my test environment at home I need to create a Server 2016 PowerShell script that will launch the Office 365 PowerShell command line commands and automatically sign on with an Office 365 administrator username and password.

This isn't a security hazard since this will only be used on an Office 365 test domain on a computer at home that no one else has access to.

Please provide me with the correct syntax for doing this.
IT GuyNetwork EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
This is for connecting to exchangeOnline
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session

Powershell commands to run on server goes here
Remove-PSSession $Session

Open in new window

Source: https://technet.microsoft.com/en-us/library/jj984289%28v=exchg.160%29.aspx?f=255&MSPPError=-2147217396

The same logic is done for any connection to any services:
here's the another example to connecto to Msol service.

IT GuyNetwork EngineerAuthor Commented:
I need to be able to enter the Office 365 administrator username & password within this script so that when the script is run it will automatically logon with the Office 365 administrator username and password.

Please provide me with the correct syntax of the PowerShell command for doing this.
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
In my post here I explain how to securely store your password for use in a script to do exactly what you're wanting to do:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Glad to help. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.