Here is my code. The first two options work but not the third. I am not sure what I am doing wrong. I am a bit of a newbie to powershell. Thanks.
$opt = Read-Host "Type 1 to login | Type 2 to disconnect | Type 3 to Exit"
write-host $opt
If ($opt -eq '1') {
$user = "myusername"
$UserCredential = Get-Credential -credential $user
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
}
ElseIf ($opt -eq '2') {
Get-PSSession | Remove-PSSession
}
ElseIf ($opt -eq '3') {
Exit
}
.ps1file? In that caseExitonly exit that.ps1file, not PowerShell itself.PowerShell.execommand line and it will do what you want. If you run PowerShell and then ran the script from within, it won't close the shell.