########################################################################################
###Script : To Execute PowerShell script with other credentials without prompts
###Developer : Chendrayan Venkatesan
###Company : Tata Consultancy Service
########################################################################################
#SharePoint Admin Account
$SPAdmin = "name@myemailaddress.com.au"
$Password = Get-Content C:\securestring.txt | convertto-securestring
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $SPAdmin, $Password
Get-WmiObject -Class Win32_Service -ComputerName "Server" -Filter "Name='ServiceName'" -Credential $Credential
$LiveCred = Get-Credential
Connect-MSOLService -Credential $cred
Connect-MSOLService -Credential $cred
I am prompted for a username and password?
Or here: https://gallery.technet.microsoft.com/scriptcenter/Execute-PowerShell-Script-38881dce