Part 2. Getting to know PowerShell (Terminal, Console and Shell)

24 July 2023 13 minutes Author: Lady Liberty

PowerShell Pro: Mastery of the Terminal, Console, and Shell

Terminal, console, and shell are important components of PowerShell that allow users to interact with the Windows operating system through commands and scripts. PowerShell provides a powerful interface to manage the system and perform various tasks, helping to increase productivity and efficiency. A terminal is a text-based interface that allows a user to enter commands and receive responses from the operating system. It is the starting point for interacting with PowerShell, where the user can run commands and view the results. The console is a window that displays the results of executed commands, error messages, and other important information. It provides a convenient way to view results and interact with the command shell. A command shell (interpreter) is software that executes commands and scripts entered by the user in a terminal or console.

PowerShell has its own command shell that allows you to perform various tasks such as managing files, registries, services, network and much more. In the PowerShell Terminal, Console, and Shell Guide, we’ll cover the basic commands and features that will help you get comfortable working with PowerShell. You’ll learn about different ways to interact with the shell, including running scripts, running commands in the background, and more. Our guide will help you quickly learn the basics of PowerShell and become more confident in using this powerful tool.

A shell for terminals, consoles and commands

In the previous section, we talked about the command line mode in Windows and the two different command shells of this operating system, mentioned about working in terminals and console applications. For effective and convenient work in the command line, it is important to choose the right tool, and for this you need to clearly understand what these terms mean.

The word itself comes from the verb terminate and means “terminal device”, that is, a device that is at one end in the process of communicating with another device (server). The task of the terminal is to send text entered from the keyboard to the server and output text responses from the server.

The first terminals in the 1950s and 1960s were connected via telephone lines to large computers. These were electric typewriters (teletypewriters, TT U). Teletypewriters typed commands and server responses were printed line by line on a roll of paper (Fig. 2.1).

Fig. 2.1. Terminal in the form of an electromechanical teletype

Later, instead of a printer, computer terminals began to use the device itself with a built-in keyboard and monitor called a console. The word console appeared long before the invention of computers and meant a bracket or a stand for something. Then consoles with buttons and switches for controlling electrical devices began to be called consoles (Fig. 2.2).

Fig. 2.2. Terminal (console) DEC I 100 (1978)

Thus, a console is a device, and a terminal is a communication program inside the console that performs the following tasks:

  1. Recognition and acceptance of characters typed by the user on the keyboard.

  2. Forming a single line from received characters, taking into account control codes (for example, moving the cursor or deleting a character).

  3. Exchange of text with a computer using communication equipment through a direct physical connection or communication lines.

  4. Displays the text received from the computer on the display. The terminal must recognize and process the so-called ANSl exit codes to set the format, color and other parameters of the text output (for example, moving the cursor to an arbitrary position on the screen).

This mechanism, when a terminal sends characters to a program running on a computer and displays the text received from this program on the screen, remains to this day the basic model of human-computer interaction through the command line.

Since the mid-1980s, hardware consoles and terminals have been superseded by personal computers and are now terminals and terminals in operating systems.

salts are called software analogues of TTU. These are programs that allow you to enter character commands, send those commands to another process, and display strings of text from that process.

Commands coming from the terminal are executed by a special program called a command shell. Depending on the received command, the shell performs certain actions and generates strings of characters that are sent back to the terminal for display on the screen.

For each operating system, there are different shells that differ in the set of commands. In systems (Linux and macOS) they are most often used with bash, zsh, fsh, tsh shells. Windows 10, as we saw in Chapter 1. There are two standard shells: cmd.exe (command line) and Windows PowerShell.

It is important to understand that command shells do not have their own user interface, they are not terminals. You can work with the same shell using different terminals, and different shells can be launched from the same terminal.

Terminals in Windows

A terminal and a shell are two applications running on the same computer that need to exchange text with each other. In such cases, this problem is solved with the help of a pseudo-terminal (Pseudo TT U, RTU), which provides two virtual devices: a slave and a master. A terminal application is connected to the main pseudo-device. And the command shell or another console application to the remote. When the terminal application sends characters to the input of the master device, they are redirected to the output of the slave device. Lines of text formed by a shell or application are sent to the input of the slave device and redirected to the output of the master device (Fig. 2.3).

Fig. 2.3. Terminal interaction

At the same time, the slave device emulates the behavior of the hardware terminal, selecting certain control combinations of symbols from the input stream and sending signals corresponding to these combinations to the connected application.

This mechanism has been in use since the 1980s, supporting the operation of terminology applications (including full-screen console devices that allow for control over

In Windows, terminal emulation is implemented differently. Recall that all modern versions of Windows are descendants of the original Windows NT operating system, developed by Microsoft in the early 990s. Work in the command line mode in Windows NT was carried out using the Windows Console plug-in associated with the cmd.exe shell (command interpreter). This standard console has continued to be used in Windows for almost 30 years.

Standard Windows console

Functionally, the Windows console terminal (ConHost) is similar to the traditional one, but arranged differently, without the use of an RTU pseudo-terminal. If the user wants to work with the command line in, he first starts a terminal. which establishes a connection to the default shell. In Windows, the user does not run the terminal itself (starting with Windows 7, it is represented by the file conhost.exe), but an executable file with a shell (cmd.e.xe or powershell.exe)  or another console utility (Fig. 2.4). At the same time, the operating system automatically connects this shell or utility to the existing IЈII and the new instance of the ConHost console.

Fig. 2.4. Command line (cmd exe) in the standard Windows console

At the same time, the ConHost process interacts with the shell using system messages (I/O Control messages, IOCTL messages) through a special driver, and not through text streams through a PTY pseudo-terminal, as in UNIX systems.

Therefore, the mechanism for supporting console programs in Windows has significant differences from the approach adopted in UNIX systems.

  • For any command shell or command line utility running, the Windows operating system always assigns the standard ConHost console (conhost.exe) as the terminal.

  • Communication channels between the console and utilities are created by the operating system itself.

  • Command-line utilities interact with the console by calling functions from the Win32 console API, such as setting the text and background color, moving the cursor to the desired position, and so on.

  • These architectural features of the Windows console have become a source of problems over time.

  • It is difficult to create alternative terminal software for Windows. The developers of such applications (pay attention here to the SopEti, Cmder, Console2, Hyper terminal emulators) had to launch a standard Windows console in a window outside the field of view of the monitor, send the characters entered by the user to it, and then read the lines returned by the shell from this window and display them in its own window.

  • About Mapsole’s Win32 APIs, which use Win32 functions to work with the console, are difficult to port to other platforms where the console is controlled via a symbolic This inconsistency in handling Tayuke’s console makes porting to Windows difficult.

  • Windows 10 supports the Windows Subsystem for Linux (WSL), which allows you to install one of the Linux distributions inside Windows and use shells and standard utilities from that operating system. At the same time, control messages coming from these utilities may be processed incorrectly in the Windows console.

  • You are having problems making Windows command line connections to the server from remote terminals on other computers. After all, in this case, you need to remotely call Win32 Console API functions, and the client machine may not work under Windows.

To solve such tasks, Microsoft developers added the SoptRTU pseudo-console infrastructure to Windows in 2018, while maintaining backward compatibility with the traditional ConHost. ConHost now fully supports utilities that use UTF-8 encoding and ANSI sequences to control the terminal (due to this, for example, full-screen files run in a WSL session are correctly displayed in the Windows console). In addition, it is now possible to create alternative terminals for Windows that work through SopRTU.

Windows Terminal

In 2019, Microsoft introduced a new and improved terminal for Windows called Windows Terminal. It is open source software (source code hosted on GitHub: https://github.com/microsoft/terminal), which is actively developed and positioned by Microsoft as the main tool for working with various shells and command line utilities in modern versions of Windows.

Let’s list the main features implemented in Windows Terminal.

  1. Tab support to open multiple shell sessions in a single window. Division of the window into several independent panels, in which you can open different sinks.

  2. The presence of a panel for entering or selecting a command to control the terminal.

  3. Support for controlling the display of text in the terminal.

  4. Full UTF-8 encoding support.

  5. Using 24-bit color.

  6. Support for graphical themes and translucent backgrounds in the terminal.

  7. Support for different display modes of the terminal window.

  8. Select the hyperlink in the text displayed in the terminal.

  9. Copy text to the system clipboard in HTML and RTF formats.

Installation and launch

The easiest way to install Windows Terminal is from the Microsoft Store (you can open it using a shortcut in the Start menu or in a browser using the link https://wyvw.microsoft.com/ru-ru/store/apps/windows) (Fig. 2.5). Other installation options are described in the terminal repository on GitHub (https:// github.com/microsoft/terminal).

Fig. 2.5. Windows Terminal in the Microsoft Store

After installation, a Windows Terminal shortcut will appear in the Start menu. To launch the terminal, you can use the Windows terminal shortcut in the “Start” menu or press the key combination <Win> + <R> and enter the name of the wt file of the terminal startup in the “Run” window. A new Windows PowerShell terminal window will open (see Figure 2.6).

Fig. 2.6. Windows PowerShell in Windows Terminal

Let’s take a look at the new features of Windows Terminal that were not present in the previous terminal.

Working with tabs

To create a new tab with the PowerShell shell, you need to click on the + icon or press Ctrl+Shift T

If you click on the v icon, a list will open where you can choose a different profile (command shell) for the new tab (Fig. 2.7):

  • Standard command line Command Prompt (cmd.exe interpreter);

  • Windows PowerShell;

  • The bash shell of the Linux operating system (if the WSL subsystem is installed and configured).

Fig. 2.7. Selecting a command shell for a new terminal tab

Splitting the window into several panels

The window in each tab can be divided into several panels, both vertically and horizontally. This allows you to view multiple command line sessions simultaneously without having to switch between tabs (see Figure 2.8). Split vertically opens a new panel to the right of the selected panel and splits horizontally below the selected panel.

Fig. 2.8. Division of the terminal window into independent panels

You can use keyboard shortcuts to divide the window into panels

Table 2.1. Splitting the window into a panel

If several panels are open in the tab, you can switch between them either with the mouse or with the arrow keys while holding down the <Alt> key. You can resize the panels by holding <Alt>+<Shift> and using the arrow keys.

Using the command palette

Various terminal control commands can be executed not only using key combinations, but also entered or selected in the command palette, which is called by pressing the keys <Ctrl>+<Shift>+<p> (Fig. 2.9).

Fig. 2.9. Windows terminal command palette

Launch a terminal with command line arguments

To start a new instance of Windows Terminal from the command line, use the wt command. At the same time, with the help of additional command arguments, you can set the current directory in which the terminal will be opened, automatically create new tabs or split the tab into several panels. Terminal commands are separated by semicolons.

For example, the following command:

wt -d С:\ ; split-pane -р ’’Windows PowerShell” ; split-pane -H wsl.exe

It will launch a new terminal with three panels on a tab:

  1. First, in the root C:\ opens the default profile, PowerShell (command -d C: \).

  2. Then the panel is split vertically and PowerShell is opened in the right half in the user’s home directory (command split-pane -r “Windows PowerShell”)

  3. Finally, the right pane is split horizontally, and the bottom half opens the WSL subsystem’s bash interpreter (split-pane -n wsl.exe command).

  4. A description of other arguments that can be specified when starting the terminal can be found in the documentation on the Microsoft website (https://docs.microsoft.com/ruru/windows/terminal/command-line-arguments).

Results

  • Hardware terminals allowed communication between computers through the command line at the very beginning of the computer era. Before the advent of personal computers, consoles were used to work with servers in the command line mode – devices with a built-in keyboard and monitor, in which a software analogue of the terminal was launched.

  • A terminal program allows you to enter character commands, sends them to another process, and displays lines of text coming from that process.

  • Commands coming from the terminal are executed by the command shell. Shells do not have their own user interface.

  • For a personal computer, a terminal and a command shell are two programs running on the same computer that need to exchange text with each other. You can work with the same shell using different terminals, and different shells can be launched from the same terminal.

  • Windows includes two command shells: the standard cmd command line and Windows PowerShell.

  • Windows uses the ConHost console as a standard terminal to which the operating system automatically connects a running shell or console application. Alternatively, you can install an enhanced Windows Terminal on Windows. This is an open source application that is actively developing and is positioned by Microsoft as the main tool for working with command line shells in modern versions of Windows.

Thanks to our team of volunteers for providing information from open sources.

Other related articles
Found an error?
If you find an error, take a screenshot and send it to the bot.