How to initiate a Remote Desktop Session from Powershell

To initiate a RDP session from a script try this:

Create a new RDP Connection

Enter all the required details and then save the connection

Then you can run it from you script using this:


$RDPServer = "c:\RDP\Server1.rdp"
Write-Verbose " Invokeing $RDPServer Session"
invoke-Expression "mstsc.exe '$RDPServer'"

If you want your script to wait for the RDP session to exit before it continues then add this:

$mstscProcess = (Get-Process | Where-Object { $_.ProcessName -ieq "mstsc" })
Write-verbose "Waiting for MSTSC.EXE to exit"
$mstscProcess.WaitForExit()
Write-verbose "MSTSC.EXE ended"

Advertisement
Follow

Get every new post delivered to your Inbox.