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.