Create multiple RDP files with Powershell

image

RDP – Remote Desktop Connection, or what we called back in the day, Terminal Services.  Every system administrator uses it daily.  At work, the team I work on has 145 servers that we are responsible for, and only a handful of them are in the same building as us (although we still remote into them as well).

This being said, I was getting tired of typing in the server’s name or IP address every time I needed to connect to it.  I know Microsoft has an mmc for Remote Desktops, but I just don’t like the clunky way it has to be setup, plus, did I mention I had 145 to put in?  Well, call me lazy, but I knew there had to be a way to script it – after all, a RDP file is nothing but some text (open a .rdp file with notepad – you’ll see!).

So, enough background, lets do some scripting!  There are 3 files here:  Powershell script, a CSV file, and a Text file.

  • First, the CSV File:  It has 4 columns with the first row being the headers.  Do not change row.  Name this file RDPLIST.csv
  • ServerName,IP,Directory,USER
    FRIENDLYNAME,FQDNorIP,FOLDERNAME\SUBFOLDER,DOMAIN\USERNAME
  • Next, is the Text File.  This contains other parameters that are going to be the same between every connection – such as enabling shared clipboard, 16bit colors, disable themes, etc.  You can customize this to your liking (to get it just the way you want it, create an .rdp file, and edit it with notepad, and you will see a list similar to below). Name this file template_bottom.txt
  • audiomode:i:2
    authentication level:i:0
    autoreconnection enabled:i:1
    bitmapcachepersistenable:i:1
    compression:i:1
    disable cursor setting:i:0
    disable full window drag:i:1
    disable menu anims:i:1
    disable themes:i:1
    disable wallpaper:i:1
    displayconnectionbar:i:1
    keyboardhook:i:2
    redirectclipboard:i:1
    redirectcomports:i:0
    redirectdrives:i:0
    redirectprinters:i:0
    redirectsmartcards:i:0
    session bpp:i:16
    prompt for credentials:i:0
    promptcredentialonce:i:1
  • Lastly, the Powershell script: Name this file CreateRDP.ps1
  • $List = Import-CSV RDPLIST.CSV
    $resolutions = ("Console","fullscreen","1024x768","1152x864")
    ForEach($Entry in $List) {
      ForEach($resolution in $resolutions) {
        # Prepend the destination directory info for RDP files
        $Dir = "..\" + $resolution + "\" + $Entry.Directory
    
        # Create new folder
        New-Item -Path $Dir -ItemType Directory -Force
    
        # Build the file name
        $FileName = $Dir + "\" + $Entry.ServerName + ".RDP"
    
        # Remove the old file
        Remove-Item $FileName -Force
    
        # Begin building RDP file
        $temp = "`nfull address:s:" + $Entry.IP 
    
        switch ($resolution) {
    
          "Console" {
            $temp = $temp + "`nscreen mode id:i:1"
        $temp = $temp + "`ndesktopwidth:i:1024"
            $temp = $temp + "`ndesktopheight:i:768"
        $temp = $temp + "`nusername:s:" + $Entry.USER
        $temp = $temp + "`nadministrative session:i:1"
          }
    
          "Fullscreen" {
            $temp = $temp + "`nscreen mode id:i:2"
        $temp = $temp + "`nusername:s:" + $Entry.USER
          }
    
          "1024x768" {
            $temp = $temp + "`nscreen mode id:i:1"
            $temp = $temp + "`ndesktopwidth:i:1024"
            $temp = $temp + "`ndesktopheight:i:768"
        $temp = $temp + "`nusername:s:" + $Entry.USER
          }
    
          "1152x864" {
            $temp = $temp + "`nscreen mode id:i:1"
            $temp = $temp + "`ndesktopwidth:i:1152"
            $temp = $temp + "`ndesktopheight:i:864"
        $temp = $temp + "`nusername:s:" + $Entry.USER
          }
        }
        $temp | out-file $FileName
        write-host $temp
        get-content template_bottom.txt >> $FileName
      }
    }

Now, you need a folder structure:

  • Powershell_RDP
    • rdp
      • _script

      image

    Put all three files in the _script folder.

And just because I’m a nice guy, here’s a zip of the files and the correct folder structure.  Just unzip, make your edits to the csv file, save and close it, and open PowerShell (as Administrator if using Vista) and navigate to the _Script folder.  Once there, run the CreateRDP.ps1 (put a ./ in front of the filename)

image

Feel free to customize this script to your own liking!  It’s a simple script, so it shouldn’t be too hard to mess with.  It works like a CHARM!!!  I created 290 RDP files in less than 30 seconds!  Great thing is, it creates a separate folder structure for the different resolutions (I used 1024×768 & 1152×864 because my laptop is a widescreen, but when I’m docked, it’s a standard monitor).

One more tip, I made two new toolbars, and pushed them all the way over to the right of the taskbar, making a nifty little launcher!

image

Hope you enjoy this – I know I did!  It took me longer to write this post than it did to get all my .RDP files! :)

[ UPDATE:  Original Script was written by Aaron Dodd, just tweaked by me]

This entry was posted in Microsoft, PowerShell, Scripts. Bookmark the permalink.

13 Responses to Create multiple RDP files with Powershell

  1. Pingback: PowerShell Team Blog : RDP File Generation/Use of HERE-Strings

  2. one1zero1one says:

    Thanks for the example, very useful in fact (I used to maintain a lost of RDP manually, but this solves the issue of updating the info).
    Just one remark, when running it on XP,
    18: $temp = “`nfull address:s:” + $Entry.IP
    should be
    18: $temp = “full address:s:” + $Entry.IP
    otherwise the mstsc won’t get the ip adress and will ask for it.
    Other then that works like a charm.

  3. apperrault says:

    Two questions. Is there a way to always use Full Screen for the connections, or do i just need to set the window size to reflect Full Screen. Also, is there a way to force a connection to the Console, rather than a regular MSTSC connection?

    Great script though.

    app

  4. admin says:

    @ apperrault:

    Yes, you can go fullscreen. Use the following changes.

    Change line 2 to read:
    $resolutions = (“fullscreen”,”1024×768″,”1152×864″)

    Insert between 20 & 22:

    “fullscreen” {
    $temp = $temp + “`nscreen mode id:i:2″
    }

    Glad you liked it! :)

  5. apperrault says:

    Thanks. What about connecting to the console (mstsc /console) vs the regular mstsc?

    Thanks for the help

    app

  6. admin says:

    Great Question!
    You can add these into the mix:

    connect to console:i:1 (if you can use /console normally)
    or
    administrative session:i:1 (if you are latest ver. of RDP requiring /admin)

    This can be added to the template_bottom.txt file, or you can get creative, and add it into the powershell script (in the same way that you added the full screen).

    Good luck~!

  7. Pingback: RDP File Generation/Use of HERE-Strings | MS Tech News

  8. woodysgate says:

    This is fantastic, but how do i format the CSV file to create multiple connections.

    I can get it to create one file just fine. When I try to create multiple files it gives me the finger and creates a default connection.

  9. Nerd says:

    woodysgate, I send you an email with the csv attachment, but in case you don’t get it, and for everyone else’s benefit, here is the csv content:

    ServerName,IP,Directory,USER
    mcanotify,mcanotiry.w10,Administration,administrator
    RightFax,sdca0112,Administration,administrator
    sdca0210,sdca0210,Administration,administrator
    sw100354,sw100354,Administration,administrator

    Hope this helps!

  10. woodysgate says:

    Ok I figured out what its doing. It is not giving each file a unique name. It is creating a file named .RDP with nothing in front therefore it keeps overwriting.

    I accidentally changed the Filename header to File name.

  11. Pingback: Save Password in .RDP File | Everyday Nerd

  12. Michael Hunt says:

    That is an awesome script! I will totally use it! Thank you for taking the time to post it.

  13. Frank says:

    I am having problems with your script.
    I did everything according to the instructions and I am ending up with the different directories containing just one file that does not connect to anything.
    I made the csv file with the hostnames in columns 1 and 2, empty dir column and username and hashed pwds in colims 4 and 5.
    The script that includes the passwords has a typo “Cullscreen” instead of “Fullscreen”. I changed this but still the script does not generate the desired connections.