Configure powershell window to open and automatically run o365 connect script

I;m trying to configure a powershell windows so that when it opens it will automatically run a script to connect to the o365 tenant. But I want it to auto populate my ID and password so I don't have to every time.

I have configured a powershell profile to use these commands, but how do add in my user Id and password?

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
rdefinoAsked:
Who is Participating?

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

x
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.

David Johnson, CD, MVPOwnerCommented:
here is my complete connect to office 365 Exchange, Sharepoint,Lync used many resources to build this script
function connect-service {
$securepassword = ConvertTo-SecureString -string "P@ssw0rd" -AsPlainText -Force
$credential = new-object System.Management.Automation.PSCredential ("user@contoso.com", $securepassword)
#$credential = Get-Credential
Import-Module MsOnline
Connect-MsolService -Credential $credential
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://techsupport4me-admin.sharepoint.com -credential $credential
Import-Module LyncOnlineConnector
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection
Import-PSSession $exchangeSession -DisableNameChecking
$ccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $ccSession -Prefix cc
}
function disconnect-service 
{
Remove-PSSession $sfboSession ; Remove-PSSession $exchangeSession ; Remove-PSSession $ccSession ; Disconnect-SPOService
}

<#
.Synopsis
  Connect-Office365
.DESCRIPTION
   Connects Session to Office 365
.EXAMPLE
   connect-office365 -connection connect
.EXAMPLE
   connect-office365 -connection disconnect
.INPUTS
   None
.OUTPUTS
   None
.NOTES
   General notes
.COMPONENT
   The component this cmdlet belongs to
.ROLE
   The role this cmdlet belongs to
.FUNCTIONALITY
   The functionality that best describes this cmdlet
#>
function Connect-O365
{
    [CmdletBinding()]
     
    Param (
        [Parameter(Mandatory=$true,Position=1)]            
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("Connect", "Disconnect")] 
        [string]$Connection
    )
if ($connection -eq 'Connect')
        { write-host 'Connecting to Office 365 session'
          write-host 'Do NOT close this Window while session active'
        connect-service }
        else { 
        write-host 'disconnecting from Office 365 Session'
        disconnect-service 
        }
    }

Open in new window

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
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
Powershell

From novice to tech pro — start learning today.