Lots of places explaining how to do this but in summary
you may have to change your execution policy settings ( Set-ExecutionPolicy bypass )
also remember if you have MFA enabled you will need to make an app password or use conditional MFA
# connect to exchange server online $UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -allowRedirection Import-PSSession $Session
To save your password as a secure string
$credential = Get-Credential $credential.Password | ConvertFrom-SecureString | Set-Content saved_encrypted_password1.txt
To read the password back and use it
$username="user@domain.com" $secureStringPwd = get-Content saved_encrypted_password_1.txt | ConvertTo-SecureString