Chapter One:
PowerShell Components
In this chapter we will explore the components of PowerShell 7.0. The components of PowerShell are the user interface (UI) with which users interact with PowerShell as well as related software that houses PowerShell components used to enhance the user interface. The related software comprises Visual Studio Code (VS Code) and the PowerShell Gallery. VS Code is a script editor that houses PowerShell integrated scripting environment features. The PowerShell Gallery is a PowerShell content central repository which can be accessed from PowerShell. In this chapter we will also go through the steps of downloading and installing the first two components (namely, PowerShell and VS Code). The installation instructions assume that you have an internet connection.
PowerShell 7.0 is a GitHub project that was made open source in 2016, and this chapter will look at how to source material available on GitHub when starting PowerShell 7.0. The main PowerShell 7.0 installation method described for the Windows and Linux operating systems uses the .NET Core SDK (Software Development Kit). In the case of macOS, the main method is by direct download, but the .NET Core SDK method will also be described. In the case of ARM, (Acorn RISC Machine) devices, a Windows method and a Linux method will be outlined.
Installing PowerShell 7.0 on Windows
The simplest method to install PowerShell 7.0 on Windows is to use the .NET Core SDK ("Installing Powershell Core On Windows - Powershell", 2019).
The first step is to have .NET Core SDK installed. This is done by downloading an installer for your Computer Processing Unit (CPU), namely, (x64 or x86) from
https://dotnet.microsoft.com/download/dotnet-core/3.1
("Install .NET Core SDK On Windows, Linux, And macOS - .NET Core", 2019).
The second step is to run the installer and follow the Wizard prompts. The third step is to check that everything installed properly by opening a command prompt and running this command (".NET Tutorial | Hello World In 10 Minutes", 2019):
>dotnet
If “dotnet” results in a dotnet help file printout, then the installation ran successfully. If the command results in an error command stating that dotnet is not recognized as a command, then try re-opening the command prompt.
The fourth step is to run this command to install PowerShell as a .NET Global tool ("Installing Powershell Core On Windows - Powershell", 2019):
>dotnet tool install --global PowerShell
The remainder of the methods can be found at:
Installing PowerShell 7.0 on Linux
PowerShell 7.0 support is available for Ubuntu (16.04, 18.04, 18.10 and 19.04), Debian (8.9.10), CentOS 7, Fedora (27,28), Red Hat Enterprise Linux (RHEL) 7, openSUSE (42.3, LEAP 15) and Arch Linux ("Installing Powershell Core On Linux - Powershell", 2019). The simplest method to install PowerShell 7.0 on Linux is to use the .NET Core SDK ("Installing Powershell Core On Linux - Powershell", 2019).
The first step is to install the .NET Core SDK. This is done by downloading a binary for your Linux distribution from https://dotnet.microsoft.com/download/dotnet-core/3.1. Then issue the following commands on the terminal ("Installing Powershell Core On Linux - Powershell", 2019):
$mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-3.1.100-linux-x64.tar.gz -C $HOME/dotnet
$export DOTNET_ROOT=$HOME/dotnet
$export PATH=$PATH:$HOME/dotnet
The preceding exports in the last commands are temporary. In other words, the .NET Core Command Line Interface (CLI) features are available only for that session. However, the commands can be made permanent by first editing the shell profile. In different Linux shells, the following profiles can be edited ("Installing Powershell Core On Linux - Powershell", 2019):
●
Bash Shell: the ~/.bash_profile and ~/.bashrc
●
Korn Shell: ~/.kshrc or .profile
●
Z Shell: the ~/.zshrc or .zprofile
Then complete the update by updating the shell source file by adding $HOME/dotnet at the end of the PATH statement. If there is no PATH statement in the file, then add the following new line:
export PATH=$PATH:$HOME/dotnet
and
DOTNET_ROOT=$HOME/dotnet
at the end of the file.
The third step is to check that everything installed properly by opening a command prompt and running this command (".NET Tutorial | Hello World In 10 Minutes", 2019):
$dotnet
If the script results in a dotnet help file printout, then the installation ran successfully. If the command results in an error command stating that dotnet is not recognized as a command, then try re-opening the command prompt. The next step is to run this command to install PowerShell as a .NET Global tool ("Installing Powershell Core On Linux - Powershell", 2019):
$ dotnet tool install --global PowerShell
Installing PowerShell 7.0 on macOS
Once PowerShell is installed, issue the following command ("Installing Powershell Core On Macos - Powershell", 2019):
$pwsh
The next best method is to install PowerShell using the .NET Core SDK ("Installing Powershell Core On macOS - Powershell", 2019). The first step is to download the installer from ("Install .NET Core SDK On Windows, Linux, And Macos - .NET Core", 2019)
https://dotnet.microsoft.com/download/dotnet-core/3.1
.
Then use the macOS standalone installer command prompts to install the .NET Core 3.1 SDK. The third step is to check that everything installed properly by opening a command prompt and running the following command (".NET Tutorial | Hello World In 10 Minutes", 2019):
$dotnet
If it results in a dotnet help file printout, then the installation ran successfully. If the command results in an error command stating that dotnet is not recognized as a command, try re-opening the command prompt.
The fourth step is to install PowerShell as a .NET Global tool ("Installing Powershell Core On Macos - Powershell", 2019) with the following command:
$dotnet tool install --global PowerShell
Installing PowerShell 7.0.0 on ARM
PowerShell 7.0 can be used on a limited set of ARM devices, which include Windows 10 ARM32/ARM64 as well as Raspbian. PowerShell 7.0 for ARM is an unsupported experimental release. In this section we will consider the installation instructions for Raspbian and Windows 10 Internet of Things (IoT).
Deployment on Windows IoT
The installation instructions for deploying on Windows IoT are as follows ("Installing Powershell Core On Windows - Powershell", 2019):
Windows IoT has Windows PowerShell pre-installed, which can be used in the installation of PowerShell 7.0.0-rc.1.
-
Creation of PSSession to device
$s = New-PSSession -ComputerName <deviceIp> -Credential Administrator
-
Copy ZIP package to device
# change the destination to however you had partitioned it with sufficient
# space for the zip and the unzipped contents
# the path should be local to the device
Copy-Item .\PowerShell-<version>-win-<os-arch>.zip -Destination u:\users\administrator\Downloads -ToSession $s
-
Connect to device and extract the archive
Enter-PSSession $s
Set-Location u:\users\administrator\downloads
Expand-Archive .\PowerShell-<version>-win-<os-arch>.zip
-
Remoting setup to PowerShell 7.0.0-rc.1
Set-Location .\PowerShell-<version>-win-<os-arch>
# Be sure to use the -PowerShellHome parameter otherwise it'll try to create a new
# endpoint with Windows PowerShell 5.1
.\Install-PowerShellRemoting.ps1 -PowerShellHome .
# You'll get an error message and will be disconnected from the device because it has to restart WinRM
-
Connect to PowerShell Core 7.0 device endpoint
# Be sure to use the -Configuration parameter. If you omit it, you will connect to Windows PowerShell 5.1
Enter-PSSession -ComputerName <deviceIp> -Credential Administrator -Configuration powershell.<version>
Installing on Raspbian
The installation on Raspbian is done by running the following sequence of commands ("Installing Powershell Core On Linux - Powershell", 2019):
###################################
# Prerequisites
# Update package lists
sudo apt-get update
# Install libunwind8 and libssl1.0
# Regex is used to ensure that we do not install libssl1.0-dev, as it is a variant that is not required
sudo apt-get install '^libssl1.0.[0-9]$' libunwind8 -y
###################################
# Download and extract PowerShell
# Grab the latest tar.gz
wget https://github.com/PowerShell/PowerShell/releases/download/v7.0.0/powershell-7.0.0-linux-arm32.tar.gz
# Make folder to put powershell
mkdir ~/powershell
# Unpack the tar.gz file
tar -xvf ./powershell-7.0.0-linux-arm32.tar.gz -C ~/powershell
# Start PowerShell
~/powershell/pwsh
PowerShell Console
In this section we will briefly consider the PowerShell Console. The PowerShell Console is a very important PowerShell component because most of the scripting will be done using the console. Later, when discussing Visual Studio Code, we will explore how the console can be linked to the Visual Studio Code environment.
The command line options used to start PowerShell can also customize the session and control the input. These features will be explored in later sections and chapters as they arise. In this exploratory tour, we can consider the basic PowerShell command line interface which will appear as follows in Windows under the C:\ path:
PS C:\>
and analogously, according to command line system file representation, for Linux and macOS under /home/user/ path:
PS /home/user/>
The basic convention in this book will be to represent the PowerShell 7.0 command line prompt as follows:
>
However, output obtained for PowerShell running in one operating system is translatable to an analogous output obtained via another operating system.
A simple administrative command to run on the console is to invoke the Get-Process cmdlet. The cmdlet displays management information on local or remote computer processes or. The PowerShell Console command to invoke the cmdlet is:
>Get-Process
The output obtained is as follows (where only the first few lines are shown):
PS C:\> Get-Process
Handles
|
NPM(K)
|
PM(K)
|
WS(K)
|
CPU(s)
|
Id
|
SI
|
ProcessName
|
-------
|
------
|
-----
|
-----
|
------
|
--
|
--
|
-----------
|
524
|
29
|
21876
|
16508
|
1.64
|
52736
|
0
|
AbtSvcHost_
|
370
|
18
|
3880
|
2356
|
3.23
|
3532
|
0
|
armsvc
|
204
|
12
|
9920
|
13424
|
0.14
|
47940
|
0
|
audiodg
|
326
|
31
|
14596
|
34876
|
0.33
|
13960
|
219
|
backgroundTaskHost
|
284
|
20
|
11776
|
25393
|
0.47
|
47300
|
216
|
backgroundTaskHost
|
133
|
9
|
2380
|
988
|
0.11
|
15092
|
0
|
CNTAoSMgr
|
The cmdlet invoked without parameter entries obtains management information on local computer processes. As you will see in the next chapter, the default mode invocation of the Get-Process cmdlet yields, for each process, an object with process management information. The Get-Process cmdlet also provides support for methods for starting and stopping the process.
This is the default method of interacting with Powershell via the console. PowerShell has more than 1900 cmdlets ("Powershell", 2020) and the basic method to invoke their functionalities, in a computing system environment using PowerShell, is through the console.
Visual Studio Code
In this section of PowerShell components, we will explore the Visual Studio Code installation and setup processes on Windows, Linux, and macOS. This section will also discuss how to locate the PowerShell Extension Code via examples. The setup process will include installing extensions for C#, .NET Core, and PowerShell.
The extension installations will also provide brief illustrations on how to use the extensions. In this light, the PowerShell extension installation will include a pointer on how to access the VS Code PowerShell examples. Similarly, for the remaining extensions, the section will end with a brief VS Code demonstration with C# and .NET Core.
The instructions for installing VS Code and VS Code extensions on your operating system (Windows, Linux and macOS) are available from:
Figure 1 below shows an empty template of the VS Code development environment.
Installing the PowerShell Extension
When looking to use VS Code with PowerShell, the first step after installing VS Code is to install the PowerShell extension. The PowerShell extension for VS Code provides language support for PowerShell versions 3.0 and upward ("PowerShell Editing With Visual Studio Code, 2019). The capabilities added by the extension include linting analysis, definition tracking, and completions.
In order to install the extension, browse and install the extension from VS Code using the Extensions menu. On the left hand side of the VS Code window, find the Activity Bar and click on the Extensions icon. The Extensions icon appears as a four block icon consisting of three connected blocks and a detached fourth block located on the VS Code left hand icon menu bar. Alternatively, use the View: Extensions command (Ctrl+Shift+X or Cmd+Shift+X), which will show a list of the most popular VS Code extensions on the VS Code Marketplace ("Managing Extensions In Visual Studio Code", 2019).
code --install-extension myextension.vsix
The third method is to install the PowerShell extension externally from VS Code using a command line tool (PowerShell, Cmd.exe, or bash) with the following command ("Powershell Editing With Visual Studio Code", 2019):
code --install-extension ms-vscode.powershell
In VS Code Insiders edition, the command changes to:
code-insiders --install-extension ms-vscode.powershell
Example PowerShell Scripts
Example PowerShell extension scripts can be found using the path ("Powershell Editing With Visual Studio Code", 2019):
C:\Users\<yourusername>\.vscode\extensions\ms-vscode.PowerShell-<version>\examples
Here, <version> stands for the extension version.
Open the example scripts in VS Code by running the following command in either PowerShell or the command prompt ("Powershell Editing With Visual Studio Code", 2019):
code (Get-ChildItem $Home\.vscode\extensions\ms-vscode.PowerShell-*\examples)[-1]
If using VS Code Insiders, run:
$Home\.vscode-insiders\extensions\ms-vscode.PowerShell-*\examples)[-1]
Another option is to open the examples using the Command Palette loaded by pressing (Ctrl+Shift+P or Cmd+Shift+P) and selecting “PowerShell: Open Examples Folder” from the available commands.
Installing the C# extension and .NET Core extension pack
The C# extension and the .NET Core extension pack can also be installed analogously to the PowerShell extension. The .NET Core extension pack includes the C# extension and other extensions related to .NET Core.
Bonus Exercise: Create a .NET Core Application with .NET Core SDK , Visual Studio Code with C# extension, and Visual Studio .NET Core extension pack
If you did not install PowerShell using the .NET SDK option in the installation, follow the installation instructions for the .NET Core SDK outlined in that section and then continue with this exercise. If you did not install either the VS Code C# extension or the .NET Core extension pack, then install at least one of these and then continue with this exercise.
.NET Core is a modular platform for developing server apps that can be deployed across operating systems (Linux, macOS, and Windows) (".NET Core And Visual Studio Code", 2019). The C# VS Code extension adds full C# IntelliSense debugging and support to VS Code script editing.
In the following exercise we will use the VS Code editor, .NET Core SDK, and the command prompt (Cmd.exe). In addition, we will use either the VS Code C# extension or the VS Code .NET Core extension.
These are the steps for building a dotnet “Hello World” application (".NET Core And Visual Studio Code", 2019):
Step 1: C# project initialization.
Start a command shell, create a directory for your Hello World application, and navigate to the directory by typing the following:
> cd C:\
> mkdir Apps
> cd Apps
> dotnet new console
The steps and output on the command prompt will be as shown in Figure 2 below.
Step 2: Open VS Code and navigate to the project folder.
The VS Code console will be as shown in Figure 3 below.
Step 3: Running the application.
The application can be run with the following command prompt command:
> dotnet run
This will generate the output shown in Figure 4 below.
PowerShell Gallery
The PowerShell Gallery is a package repository containing modules, scripts, and Desired State Configuration (DSC) resources ("Get Started With The Powershell Gallery - Powershell", 2019). These are available for download and leverage. The PowerShellGet module has cmdlets that can be used to explore and install these packages. Cmdlets will be covered in Chapter Two: Commands. A PowerShell Gallery item download does not require signing into the gallery. The PowerShell Gallery will be covered in Chapter Eight: Remoting.
As we will consider in Chapter Eight, PowerShell Gallery packages can be obtained using the Find-Module, Find-Script and Find-DscResource cmdlets, depending on the package type, with parameter -Repository PSGallery. Result filtering can use the following parameter list: AllVersions, Command, DscResource, Filter, Includes, MinimumVersion, Name, Tag, and RequiredVersion.
The Find-Dscresource cmdlet can be used for discovering DSC resources. The cmdlet returns information on Gallery DSC resources. DSC resources are packaged in a module, and thus after the resource is found, it can be installed using the Install-Module cmdlet.
Exercises
1.1. What are the main methods to run the PowerShell Console?
1.2. What are the three VS Code extensions considered in the Chapter?
1.3. Is it possible to run the PowerShell Console from within VS Code? If so, how?
1.4. What PowerShell module can be used to install packages from the PowerShell Gallery?
1.5. Why is the PowerShell Gallery useful when using PowerShell 7.0?
1.6. How do you install the PowerShell extension from within VS Code using a .vsix file?
Chapter Summary
The key points of the chapter were:
●
PowerShell 7.0 has operating system and environment specific installation procedures.
●
The .NET Core SDK based installation process is the simplest across all supported platforms.
●
The main components of PowerShell 7.0 are the PowerShell Console, Visual Studio Code, and the PowerShell Gallery.
●
The Console and Visual Studio Code components can be linked using the Visual Studio Code PowerShell extension.
●
The main components of PowerShell constitute the GUI with which users interact with PowerShell.
●
The PowerShellGallery component houses a lot of PowerShell features that can be used to enhance the basic installation when the setting requires so.
In the next chapter you will learn about PowerShell commands and how they are issued in PowerShell when using various components in your computing environment activities.