Writing Terminal Service Based Scripts (Part 5)

If you would like to read the other parts in this article series please go to:

So far in this article series, I have shown you several commands that you can use to perform various tasks from a command line in a Terminal Service environment. My goal is to initially teach you some commands that you can use, and to eventually show you how to build some scripts using the commands that you have learned. In this article, I will continue our discussion by showing you a few more commands.

Change User

As you probably already know, the Terminal Services offer two basic modes of operation; execute and install.  Normally, when you establish a terminal service session, the session is operating in execute mode. This means that you’re free to run applications, but you’re not allowed to install applications (at least not shared applications). In fact, if you try to run an application named setup.exe while operating in execute mode, the Terminal Services will block the attempt.

Normally, the way that you would install an application in a terminal service environment involves using the Control Panel’s Add/Remove Programs applet. This doesn’t do you a lot of good if you need to install an application using an automated script though. This is where the Change User command comes into play. The Change User command allows you to see which mode you are currently operating in, and switch modes if necessary.

The syntax for the command is simple.  Just enter the Change User command, followed by a switch that tells the command what you want to do. The /Query switch causes the command to tell you what mode you’re operating in currently. The /Execute switch allows you to switch to execute mode, while the /Install switch allows you to switch to install mode.

Some scripting environments make it difficult to enter commands that consists of multiple words. That being the case, Microsoft allows you to abbreviate the Change User command as CHGUSER.


Earlier in the article series, I showed you how you can use the TSDISCON command to disconnect a terminal services session. As you might have already figured out, the TSCON command does just the opposite, it allows you to connect to a terminal service session.

Before I show you how to use this command, you need to understand a little bit about what it actually does. Contrary to the way that the command’s name sounds, this command is not establish a brand-new terminal service session. Instead, it allows you to abandon your existing session, and connect to a different session that has already established.

If you take a look at Figure A, you will notice that I used the Query Session command to display a list of all of the sessions that are currently connected to the server. Right now, I am connected to the server through sessions zero. You also noticed that session number one has been disconnected.  Although the session has been disconnected, I can still use the TSCON command to connect to it.

Figure A: I am currently connected to the server through session zero

I simply enter the TSCON command followed by the session number that I want to connect to (in this case 1), and I am instantly connected to that session. I would love to show you a screen capture of the process, but unfortunately there is nothing to show since my initial session is instantly disconnected, and I am attach to a new session. In fact, if you look at Figure B, you’ll notice that even the applications that were running prior to session number one being disconnected are now running within my connection.

Figure B: When you connect to another session, you join the session in its current state

As you can see, when you connect to another session, you join that session in its current state. If applications are running prior to your connecting to the session, you will see those applications once you connect.

There are two main things that you need to know about the TSCON command. First, you aren’t necessarily limited to connecting to sessions that have been disconnected. In some situations, it is possible to connect to an active session. In fact, take a look at Figure C. Before I started writing this article, I was initially attached to the server using session 1. I used the TSCON 0 command to disconnect session 1 and connect to session 0. If you look at the figure though, you will see that session 0 was already connected.

Figure C: It is possible to connect to a session that is already connected

The other thing that you need to know is that after you connect to another session, it is always possible to use the TSCON command to disconnect from that session, and go back to your original session. As a matter of fact, that’s how I got the screen shot shown in Figure C. I had actually forgotten to capture the screen initially, but I was able to disconnect from sessions zero and go back to session 1, and pick up right where I left off.

So far, you to see in the simplest possible use of the TSCON command. If you look at Figure C though, you will notice that it displays the syntax for this command, and that there are several different options that you can use when connecting to a session.

Most of the time, you really won’t have to worry about using many of these options. The sessionid, sessionname, and /DEST:sessionname options just give you a variety of ways of specifying the session that you want to connect to. In my examples, I took the easy way out by just providing a session ID (the numbers 1 and 0).

One option that you may end up having to use though is the/Password option. If you are connecting to a session that you already own, then you will have to worry about specifying a password. However, if you are connecting to a session that was established by someone else, then you will have to use the/Password switch to specify that user’s password. For example, suppose that you wanted to connect to session number 12, which was established by another user. If you know that that user’s password is P@ssw0rd, then the command would look like this:


Notice that you don’t have to worry about specifying a username, because the TSCON command doesn’t really care who initiated the session as long as you have a valid password.


So far, I’ve shown you quite a few different commands that you can use one building a terminal service based script. That’s it for the commands for right now. It is possible that I may have to introduce other commands later in the series, but I have covered enough commands that we should be able to create some pretty decent scripts using them.

In the next article in this series, I’m going to give you a quick crash course in the scripting language that we will be using. I’ve already covered the terminal service specific commands, but there are a few general-purpose commands that you will need to know about before you will be able to build any kind of useful script.

If you would like to read the other parts in this article series please go to:

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top