Connect-RDPSession.ps1


Description

Connect-RDPSession starts the native Remote Desktop client (mstsc.exe) for every computer name you supply. The advanced function exposes a -ComputerName parameter (alias -cn) that accepts single or multiple targets as values or from the pipeline, and honours WhatIf / Confirm semantics thanks to SupportsShouldProcess.

Usage examples

Connect-RDPSession -ComputerName 'server01'
'rdp01','rdp02' | Connect-RDPSession -WhatIf

When a launch fails the function reports the computer as unreachable, otherwise the RDP session window opens immediately.


Script

#requires -PSEdition Desktop
Function Connect-RDPSession {
	<#
	.SYNOPSIS
		Connect-RDPSession
	
	.DESCRIPTION
		Connect-RDPSession - Spawn MSTSC and launches an RDP session to a remote computer.
	
	.PARAMETER ComputerName
		This parameter accepts the Name of the computer you would like to connect to.
		Supports IP/Name/FQDN
	
	.EXAMPLE
		Connect-RDPSession -ComputerName COMPUTERNAME
		Starts an RDP session to COMPUTERNAME
	
	.OUTPUTS
		System.String. Connect-RDPSession
	
	.NOTES
		Author:     Luke Leigh
		Website:    https://scripts.lukeleigh.com/
		LinkedIn:   https://www.linkedin.com/in/lukeleigh/
		GitHub:     https://github.com/BanterBoy/
		GitHubGist: https://gist.github.com/BanterBoy
	
	.INPUTS
		ComputerName - You can pipe objects to this perameters.
	
	.LINK
		https://scripts.lukeleigh.com
		Get-Date
		Start-Process
		Write-Output
#>
	
	[CmdletBinding(DefaultParameterSetName = 'Default',
		PositionalBinding = $true,
		SupportsShouldProcess = $true)]
	[OutputType([string], ParameterSetName = 'Default')]
	[Alias('crdp')]
	Param
	(
		[Parameter(ParameterSetName = 'Default',
			Mandatory = $false,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			HelpMessage = 'Enter a computer name or pipe input'
		)]
		[Alias('cn')]
		[string[]]$ComputerName
	)
	
	Begin {
		
	}
	
	Process {
		ForEach ($Computer In $ComputerName) {
			If ($PSCmdlet.ShouldProcess("$($Computer)", "Establish an RDP connection")) {
				try {
					Start-Process "$env:windir\system32\mstsc.exe" -ArgumentList "/v:$Computer"
				}
				catch {
					Write-Output "$($Computer): is not reachable."
				}
			}
		}
	}
	
	End {
		
	}
}

Back to Top


Download

Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top