I was pretty excited that work was finally going to install Remote Desktop Connection Manager v2.2 on our laptops, until I started using it. It is nice if you work on one server at a time but sometimes I multitask while something is running, or I need to compare something to the results on another server. You can “undock” the window and then go to full screen like you would with the regular Remote Desktop Connection, however you have to do that for each server and each time after closing it.
So I decided to go back to RDP files, using Remote Desktop Connection. I did not want to go through the process of having to create those files all over again. So guess what I wanted to do? That’s right, see if PowerShell could do it for me.
A quick search on Google I came across a post from the Windows PowerShell Blog on RDP file generation. The post is actually a review of a script someone wrote and blogged about that will create multiple RDP files with PowerShell. Which the original author of this script is lost since the link to the author of the script is dead.
Anyway, I decided to start with this script but do a few things differently. As well, the PowerShell Team mentioned one of the flaws in the guy’s script is it is dependent upon a CSV file, something developers may call “hard coded”. I wanted to make it more portable and useful for other folks to use.
My script assumes the current directory so where you run the script from is where the files will be created. I did not care about creating directories or creating RDP file with different resolution settings. If you do, you are more than welcome to adjust the script to your preferences.
The script has three (3) parameters to pass, with two being required and the third one is optional. The first position is the $server that you want to connect to in the RDP file. The second being your RDP file template (I’ll explain how I created mine). Then the third and optional parameter, is the username for that RDP file. I don’t usually put my username in the RDP file due to the environment I work in, but you may want to.
Ok, first how I created the RDP template file. I open up Remote Desktop Connection (from the run prompt: mstsc.exe). Go into the options and set everything as you like (Display, Local Resources, etc.). Then under the General tab click “Save As…”. Save the file to your desktop or where you saved the script, giving it a name of your liking. Then using Notepad open that file up and you will see something similar to this:
The highlighted line is what you want to remove and then save the file. This line will be added by the script and include your server name you pass.
So here is the script…(download here)
param(
[Parameter(Position=0,Mandatory=$True)]
[string]$server,
[Parameter(Position=1,Mandatory=$True)]
[string]$MyDefaultRDP,
[Parameter(Position=2,Mandatory=$False)]
[string]$myaccount
)
ForEach($entry in $server)
{
#build the file
#assumes current directory
$filename = “.\” + $entry + “.rdp”
# Add hostname in RDP file
$temp = “full address:s:” + $Entry
#test for username, if empty it will not be added
if ($myaccount) {
$temp = $temp + “`nusername:s:” + $myaccount
}
#check to see if file exist
if (Test-Path $filename) {
Remove-Item $filename -force
Write-Host “Found $filename existed, so I deleted it for you”
}
$temp | Out-File $filename
Get-Content $MyDefaultRDP | Out-File $filename -Append
Write-Host “$filename created”
}
[Anyone know an easy way to format PowerShell code? This is the best I could get with Windows Live Writer Add-On.]
Ok so here are some examples of how you can use this:

If you need to use an IP address for your server instead of the hostname:

Then if you want to add your username:

If you want to use a text file to pass a list of servers and different usernames I would suggest using a CSV file. Then work with the Import-Csv cmdlet to pass in each object to the right parameter.
Something like this should work for you (assume column headers in CSV file are Server and Username):
$servers = Import-Csv .\Mylist.csv
Foreach($srv in $servers){
.\New-RDP.ps1 $srv.Server .\MyDefault.rdp $srv.Username
}
NOTE: A small issue I have had running this script on my machine at home (Window 7 Ultimate) is the escape character “`n” (backtick + n) is supposed to add a new line before it writes the username text to the file. However it is not doing it when I run the script. If I have the $temp output to the console it shows the new line, but when it writes it to the file it does not for some reason. Which with the RDP file it does not matter as you can see in the screenshot above it still puts the username in the field. I don’t know if you will see the same problem, but wanted to let you know.