Chapter 3

Terminals

A terminal, or command line interface, is a text-based way to interact with your computer. Terminals can be faster and more powerful than graphical user interfaces (GUIs) for many tasks, especially tasks that involve file management, development, or automation.

This chapter provides an introduction to some widely-used terminals.

Mac and Linux

Mac and Linux systems offer the Terminal app, which provides a command-line interface for navigating the file system, running commands, and executing scripts. The Terminal supports a wide range of commands and utilities, and can be customized with various themes and configurations to suit individual preferences.

PowerShell

PowerShell is a powerful terminal and scripting language offered by Microsoft for all platforms, including Windows, macOS, and Linux. PowerShell provides a more powerful and flexible command-line environment than the Command Prompt, with support for features like object-oriented pipelines, remote management, and scripting with .NET objects.

VS Code Terminal

The terminal in Visual Studio Code offers a built-in command-line interface for developers. It provides access to a range of commands and utilities, including those specific to development tasks like running build scripts, testing code, and debugging applications.

Windows Terminals

Windows offers a variety of terminals depending on the user’s needs and preferences. These terminals include:

  • Command Prompt: A basic terminal emulator that has been included in Windows since the early days.
  • Git Bash: A terminal emulator that is bundled with Git for Windows. It provides a Unix-like command-line environment for Windows, including support for common Unix utilities and shell scripting.
  • PowerShell: A command-line shell and scripting language that is designed for system administration and automation in Windows.
  • Windows Subsystem for Linux (WSL): A feature of Windows 10 that allows users to run a Linux environment directly on Windows, without the need for a virtual machine or container.

Each of these terminals provides a unique set of features and capabilities, allowing users to choose the terminal that best fits their needs and workflow.

Subsections of Terminals

Mac and Linux

On Mac and Linux machines, the Terminal app is widely used.

Terminal is a command-line interface that allows users to interact with their computer using text commands. It provides a way to execute commands, manage files, and run scripts without the need for a graphical user interface.

The Terminal can be accessed by opening the Terminal application on a Mac or by opening a terminal emulator on a Linux distribution.

Once in the Terminal, users can navigate the file system, run commands, and install and manage software packages. The Terminal is a powerful tool for advanced users and developers, allowing for efficient and precise control over the computer. However, it does require some knowledge of command-line interfaces and syntax, which can be intimidating when getting started.

Homebrew is useful commnad line tool for managing software packages in Terminal.

PowerShell

PowerShell is a cross-platform terminal and scripting language used for a wide range of purposes including automation, administration, analytics, and digital forensics.

PowerShell includes Intellisense to help complete commands. To accept, use the right arrow key (instead of tab).

PowerShell Core

This section provides an overview of the cross-platform PowerShell Core, including some use cases and capabilities.

PowerShell vs Windows PowerShell

When we say PowerShell, we generally mean PowerShell Core, the cross-platform version. There is also Windows PowerShell. They are different.

Installation

This section helps with installing PowerShell on various operating systems, including Windows, Linux, and macOS.

Multple Versions

This section covers how to manage multiple versions of PowerShell on the same machine, and remove extra versions when no longer needed.

PowerShell in VS Code

This section covers the use of PowerShell in the popular code editor, Visual Studio Code (VS Code).

Subsections of PowerShell

PowerShell Core

PowerShell Core is the cross-platform version of PowerShell. It was developed by Microsoft as an open-source project and is designed to run on Windows, Linux, and macOS, making it a truly cross-platform tool. PowerShell Core has a similar syntax and features to the traditional Windows PowerShell, but with additional cross-platform capabilities and support for running on a variety of operating systems.

PowerShell Core also includes many improvements and new features compared to the older Windows PowerShell, including improved performance, support for Docker containers, and enhanced security features. PowerShell Core also has its own module repository, called the PowerShell Gallery, where users can download and install community-built PowerShell modules.

Here are some common use cases:

Analytics

  • Data collection and analysis: PowerShell can be used to collect and process data from various sources, including databases, logs, and web services. It can also be used to perform data analysis tasks such as data cleansing, transformation, and aggregation.

  • Reporting: PowerShell can be used to generate reports and visualizations based on data collected and analyzed from various sources. This can include dashboards, charts, and other types of visualizations.

  • Machine learning: PowerShell can be used to develop and train machine learning models using libraries such as Microsoft’s Cognitive Services and TensorFlow.

Digital forensics

  • Data acquisition: PowerShell can be used to acquire data from disk images, memory dumps, and other sources for forensic analysis.

  • Evidence examination: PowerShell can be used to search for specific file types, keywords, and other indicators of compromise within acquired data.

  • Data recovery: PowerShell can be used to recover data from damaged or corrupted drives, and to reconstruct deleted or lost files.

  • Network analysis: PowerShell can be used to analyze network traffic and investigate potential security incidents or threats.

System administration

PowerShell provides a powerful command-line interface for managing Windows operating systems, including managing users and groups, configuring network settings, installing software, and performing other administrative tasks.

Automation

PowerShell can be used to automate repetitive tasks, such as backups, file transfers, and system maintenance tasks. PowerShell scripts can be scheduled to run automatically or triggered by events such as system startup or user logon.

Development

PowerShell includes a full-featured scripting language with access to the .NET Framework and other APIs, making it a powerful tool for developing and testing applications and scripts.

Cloud computing

PowerShell can be used to manage cloud services and resources on platforms such as Microsoft Azure and Amazon Web Services (AWS). PowerShell modules and cmdlets are available for managing virtual machines, storage, networking, and other cloud services.

Security

PowerShell includes a range of security features, including execution policies, code signing, and encryption, making it a valuable tool for securing systems and managing user access to resources.

PowerShell: Core vs Windows

Windows PowerShell and PowerShell Core are two different versions of PowerShell that differ in their supported operating systems and features.

Windows PowerShell (older, Windows-specific)

Windows PowerShell is the original version of PowerShell that was released in 2006 and is included by default in Windows operating systems. It runs on the .NET Framework and is designed to work specifically on Windows operating systems. Windows PowerShell has a wide range of built-in cmdlets and supports scripting in PowerShell, as well as other scripting languages like VBScript and JScript. The typical installation path for Windows PowerShell is C:\Windows\System32\WindowsPowerShell\v1.0.

PowerShell Core (newer, cross-platform)

PowerShell Core, on the other hand, is an open-source version of PowerShell that was released in 2016 and is designed to be cross-platform. It runs on .NET Core and supports Windows, Linux, and macOS operating systems. PowerShell Core has a smaller footprint than Windows PowerShell and is designed to be more lightweight and modular. It includes many of the same built-in cmdlets as Windows PowerShell, but also has some additional features like support for SSH remoting and improved performance. The typical installation path for PowerShell Core is C:\Program Files\PowerShell\7.

Using Both

The two versions differ in their operating system support and feature set. Windows PowerShell is designed specifically for Windows operating systems and is included by default, while PowerShell Core is designed to be cross-platform and is a separate install.

PowerShell: Installation

PowerShell is a powerful cross-platform terminal and scripting language used for a wide range of purposes. Here are some options for installing PowerShell on your system:

Mac/Linux Users

Windows Users

  • Official installation instructions. Follow the instructions on the official PowerShell website to download and install PowerShell on your system.

Verify Version

Open PowerShell and run the following command to verify installation:

$PSVersionTable.PSVersion

PowerShell: Multiple Versions

There may be multiple versions of PowerShell on your computuers, especially on Windows. This can sometimes cause confusion.

It’s perfectly fine to have multiple installations, but you can remove some if they are unneeded.

On Windows machines, it’s common to have both:

  • PowerShell Core (the newer, cross-platform version)
  • Windows Powershell (the original Windows-specific verison)

Read more about Windows PowerShell vs PowerShell Core.

Uninstall

To uninstall older versions of PowerShell on Windows, follow these steps:

  1. Open the Start menu and type “Add or remove programs” in the search box. Click on the “Add or remove programs” option that appears in the search results.

  2. In the list of installed programs, locate the PowerShell versions to uninstall. Search for “PowerShell” or sort the list by name or date to find the relevant entries.

  3. Click on each PowerShell version that you want to uninstall, then click “Uninstall”. Follow the prompts to complete the process.

  4. Once uninstalled, ensure the version you want to keep is still installed and functional.

  5. Check the version of PowerShell by opening a PowerShell window and running the command $PSVersionTable.PSVersion. This will display the version number of the PowerShell installation that is currently in use.

PowerShell in VS Code

Visual Studio Code (VS Code) is a popular code editor that supports many programming languages, including PowerShell.

Terminals in VS Code

  1. Open VS Code and navigate to the “Terminal” panel by clicking on the terminal icon in the left-hand panel, or by using the keyboard shortcut Ctrl+Shift+ ``.

  2. In the Terminal panel, click on the plus (+) icon to open a new terminal.

Default Shell

By default, the new terminal will use the default shell associated with your system, e.g., Windows Command Prompt (cmd).

Open a PowerShell Terminal from Panel

To open a PowerShell terminal in VS Code, click on the drop-down arrow in the terminal panel and select “PowerShell” from the list of available shells.

Set Default Terminal

There are several options for setting the default terminal to PowerShell in VS Code:

  1. Use the Command Palette (recommended)
  2. Use the Terminal Dropdown
  3. Use the VS Code Settings

This method helps with multiple PowerShell installations when you want to open a specific PowerShell option.

In VS Code, from the menu, select View / Command Palette / Terminal: Select Default Profile.

Look carefully at the PowerShell options. For example, you may have:

  • PowerShell
  • Winddows PowerShell

the typical installation path for PowerShell Core is something like C:\Program Files\PowerShell\7.

The typical installation path for Windows PowerShell is something like C:\Windows\System32\WindowsPowerShell\v1.0.

Read more about Windows PowerShell vs PowerShell Core.

2. Set Default from Terminal Dropdown

Alternatively, to make PowerShell your default terminal in VS Code, click on the drop-down arrow in the terminal panel and select “Select Default Profile”.

  1. In the “Select Default Profile” dropdown, select “PowerShell” from the list.

3. Set Default using Settings

Or to configure the Settings directly, follow these steps:

  1. Open the VS Code settings editor by clicking on the gear icon in the lower-left corner of the window and selecting “Settings” from the menu.

  2. In the search bar at the top of the settings editor, type “terminal.integrated.shell.windows”. This will filter the settings to show the terminal shell settings for Windows.

  3. Click on the edit icon (pencil icon) next to “Terminal > Integrated > Shell: Windows” to open the editing interface.

  4. Enter the path to the PowerShell executable that you want to use as the default shell in VS Code. For example, if you want to use PowerShell Core (the cross-platform version), you might enter “C:\Program Files\PowerShell\7\pwsh.exe” (assuming that PowerShell Core is installed in the default location).

  5. Save the changes to the “Settings” editor by clicking on the “Save” button or using the keyboard shortcut (Ctrl+S on Windows/Linux or Command+S on macOS).

Run PowerShell as Administrator

Sometimes you’ll need to run PowerShell as an Adminstrator (Admin), for example, when installing packages with Chocolatey.

Outside VS Code, Start / Windows PowerShell / Run as Administrator.

Use as needed and then return to VS Code for non-admin commands.

VS Code Terminal

The Terminal in Visual Studio Code is a powerful tool.

It provides a command-line interface within the editor, allowing you to perform various tasks without leaving the editor.

Here are some of the key features of the Terminal in VS Code.

Multiple Integrated Terminals

You can have multiple terminal instances in VS Code, each running a different shell or command. This allows you to work with multiple environments at the same time.

Customizable Shell Environment

You can customize the shell environment by specifying shell-specific settings like environment variables, shell arguments, and shell location. This is useful for working with specific development environments or tools.

Keyboard Shortcuts

The Terminal in VS Code has several built-in keyboard shortcuts that can help you navigate and interact with the terminal more efficiently.

Integrated Tasks

VS Code allows you to define custom tasks that can be executed within the terminal. This is useful for automating repetitive tasks or building and testing your code.

Debugging

You can also use the Terminal in VS Code for debugging your code. You can set breakpoints and step through your code within the terminal, making it easier to identify and fix issues.

To open the Terminal in VS Code, press Ctrl+` or navigate to the View menu and select Terminal. From there, you can customize the terminal settings and start working with the command line directly within the editor.

Changing the Default Terminal in VS Code

To change the default terminal in VS Code, follow these steps:

  1. Open VS Code and go to “File” -> “Preferences” -> “Settings”.

  2. In the search bar, type “terminal.integrated.shell”.

  3. Click on “Edit in settings.json” on the right-hand side.

  4. In the “settings.json” file, add the following line:

    “terminal.integrated.shell.windows”: “C:\Path\To\Your\Terminal\Executable”

    Replace “C:\Path\To\Your\Terminal\Executable” with the path to the executable file for the terminal you want to use.

  5. Save the “settings.json” file and close it.

  6. Restart VS Code for the changes to take effect.

After following these steps, your chosen terminal will be set as the default terminal in VS Code.

Windows

Windows offers multiple terminal options for developers and users, and many are widely used. Skills in any or all of these can be very valuable.

Command Prompt

The Command Prompt is a basic terminal emulator that has been included in Windows since the early days. It supports running basic commands, navigating the file system, and running batch scripts. However, it lacks some of the advanced features found in modern terminal emulators.

Git Bash

Git Bash is a terminal emulator that is bundled with Git for Windows. It provides a Unix-like command-line environment for Windows, including support for common Unix utilities and shell scripting. It also includes Git-specific features, such as auto-completion for Git commands and syntax highlighting for diffs.

PowerShell

PowerShell is a command-line shell and scripting language designed for system administration and automation. It provides a more powerful and flexible command-line environment than the Command Prompt, with support for features like object-oriented pipelines, remote management, and scripting with .NET objects.

PowerShell includes Intellisense to help complete commands. To accept a suggestion, use the right arrow key (instead of tab).

Windows Subsystem for Linux

Windows Subsystem for Linux (WSL) is a feature of Windows 10 that allows users to run a Linux environment directly on Windows, without the need for a virtual machine or container. It provides access to a full-fledged Linux system, including a terminal emulator and support for running Linux applications and scripts. WSL can be used for development, testing, and running Linux-based tools and utilities on Windows.

Knowledge of Linux commands can be especially helpful in security and digital forensic investigations.

Subsections of Windows

Windows: Command Prompt

The Command Prompt is a basic terminal emulator that has been included in Windows since the early days. It provides a command-line interface for interacting with the file system, running basic commands, and executing batch scripts.

The Command Prompt supports a range of commands, including:

  • dir: List the contents of a directory.
  • cd: Change the current directory.
  • md: Create a new directory.
  • del: Delete files.
  • copy: Copy files.
  • move: Move files.

In addition to these basic commands, the Command Prompt supports a range of advanced features, such as redirection of input and output, piping of commands, and batch scripting.

It’s a simple and lightweight tool well-suited for basic tasks and for users who prefer a minimalist interface.

However, it lacks some of the advanced features and flexibility found in more modern terminal emulators like PowerShell and GitBash.

Windows: Git Bash

Git Bash is a terminal emulator that is bundled with Git for Windows. It provides a Unix-like command-line environment for Windows, including support for common Unix utilities and shell scripting. Git Bash includes a range of features and commands that are useful for developers working with Git repositories, including:

  • git: A command-line interface for interacting with Git repositories, including tasks like cloning, committing, pushing, and merging changes.
  • ssh: A command-line interface for managing secure shell connections to remote servers and devices.
  • curl: A command-line tool for transferring data over various protocols, including HTTP and FTP.

In addition to these Git-specific features, Git Bash supports a range of general-purpose commands and utilities, including:

  • ls: List the contents of a directory.
  • cd: Change the current directory.
  • mkdir: Create a new directory.
  • rm: Delete files.
  • cp: Copy files.
  • mv: Move files.

Windows: PowerShell

PowerShell is a command-line shell and scripting language designed for system administration and automation in Windows. PowerShell provides a more powerful and flexible command-line environment than the Command Prompt, with support for features like object-oriented pipelines, remote management, and scripting with .NET objects.

PowerShell includes a wide range of built-in commands, called cmdlets, that provide access to various Windows management features, such as:

  • Get-Process: Display information about running processes, including process ID (PID), CPU and memory usage, and parent process ID.
  • Get-Service: Display information about system services, including status, startup type, and dependencies.
  • Get-ChildItem: List the contents of a directory and display file information, including timestamps and permissions.
  • Set-Content: Write text to a file.
  • Invoke-WebRequest: Retrieve content from a web page or API.

In addition to these built-in cmdlets, PowerShell supports a range of scripting and automation features, including:

  • Variables and data types: PowerShell supports a range of data types, including strings, numbers, and arrays, as well as variables for storing and manipulating data.
  • Control flow statements: PowerShell supports a range of control flow statements, including loops, conditionals, and switch statements.
  • Functions: PowerShell supports the creation of reusable functions, allowing for modular and organized scripts.
  • Remote management: PowerShell can be used to manage remote Windows machines, allowing for automation and management of distributed systems.

PowerShell is a powerful and flexible tool for system administration and automation in Windows, providing access to a wide range of management features and automation capabilities.

See Also

There is more information about PowerShell in the Language Chapter and the Terminals Chapter.

Windows: WSL2

Windows Subsystem for Linux (WSL) is a feature of Windows 10 that allows users to run a Linux environment directly on Windows, without the need for a virtual machine or container. WSL provides access to a full-fledged Linux system, including a terminal emulator and support for running Linux applications and scripts.

WSL supports two different versions:

  • WSL 1: Uses a translation layer to provide compatibility between Linux system calls and Windows kernel system calls. WSL 1 provides access to a full Linux environment, but can be slower than running Linux natively.

  • WSL 2: Uses a lightweight virtual machine to provide a complete Linux kernel running directly on Windows. WSL 2 provides improved performance and compatibility with Linux applications, but requires more system resources.

WSL includes a range of Linux commands and utilities, allowing users to perform tasks like navigating the file system, managing packages, and running scripts. Users can also install and use Linux applications and development tools directly within WSL, including:

  • Python, Ruby, and other programming languages.
  • Git, Subversion, and other version control systems.
  • Apache, NGINX, and other web servers.
  • Docker, Kubernetes, and other containerization tools.