Launch remote PowerShell session using saved ps1 file

Solution 1:

add `-noexit'

PowerShell.exe -noexit -Command Enter-PSSession -computername Win2012SrvCore -credential administrator

Solution 2:

$passwd = convertto-securestring -AsPlainText -Force -String MYPASSWORD

$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "administrator",$passwd

$session = new-pssession -computername Win2012SrvCore -credential $cred

Add one more line:

Import-PSSession $session

Then save the .PS1 file and create a shortcut to it as powershell.exe -noexit -File "C:\PS.ps1".


Solution 3:

Try saving your commands as a script file and then have your shortcuts use the command-line:

powershell.exe -noExit <filename.ps1>

This will have your shortcuts run the specified script file and not exit powershell at the end of the scripts execution, so you can continue to use the window after the session is established.

For this to work you need to make sure the PowerShell execution policy is not Restricted, otherwise no script files can be executed

To check the current execution policy you can use Get-ExecutionPolicy and you can either use Set-ExecutionPolicy to change the policy permanantly or add the -ExecutionPolcy parameter to the powershell command-line to change it for a single session.

More information about execution policies and their impact can be found using the help about_Execution_Policies command.