What is Windows PowerShell and how is it used? Part 1: Key Features

Historically, command-line utilities in Unix systems have been more developed than in Windows, but with the emergence of new solutions, the situation has changed.

Windows PowerShell enables system administrators to automate most routine tasks. It allows you to change settings, stop and start services, and maintain most installed applications. Considering the blue window as just another command interpreter would be incorrect. This perspective does not reflect the essence of the innovations proposed by Microsoft. In reality, Windows PowerShell's capabilities are much broader: in a short series of articles, we will attempt to understand how Microsoft's solution differs from the more familiar tools.

What is Windows PowerShell and how is it used? Part 1: Key Features

Key Features 

Indeed, Windows PowerShell is primarily a command shell with a scripting language, originally built on the .NET Framework and later on .NET Core. Unlike shells that accept and return text data, Windows PowerShell works with .NET classes, which have properties and methods. PowerShell allows for the execution of standard commands while also providing access to COM, WMI, and ADSI objects. It utilizes various stores, such as the file system or the Windows registry, accessed through so-called providers. It is also worth noting the ability to embed executable PowerShell components into other applications for various operations, including through graphical interfaces. Conversely, many Windows applications provide access to their management interfaces via PowerShell. 

Windows PowerShell allows:

  • To change operating system settings;
  • To manage services and processes;
  • To configure server roles and components;
  • To install software;
  • To manage installed software through specialized interfaces;
  • To embed executables into third-party programs;
  • To create scripts for automating administrative tasks;
  • To work with the file system, Windows registry, certificate store, etc.

Shell and development environment

Windows PowerShell exists in two forms: in addition to the console emulator with a command shell, there is an Integrated Scripting Environment (ISE). To access the command line interface, simply select the corresponding shortcut from the Windows menu or start powershell.exe from the Run menu. A blue window will appear on the screen, significantly different in capabilities from the outdated cmd.exe. It features autocomplete and other functionalities familiar to users of Unix shell environments.

What is Windows PowerShell and how is it used? Part 1: Key Features

To work with the shell, you need to remember some keyboard shortcuts:

  • The up and down arrows scroll through the history to repeat previously entered commands;
  • The right arrow at the end of the line retypes the previous command character by character;
  • Ctrl+Home deletes text from the cursor position to the beginning of the line;
  • Ctrl+End deletes text from the cursor to the end of the line.

F7 displays a window with entered commands, allowing you to select one of them. The console also supports mouse text selection, copy-pasting, cursor positioning, deletion, backspace—everything we love.

What is Windows PowerShell and how is it used? Part 1: Key Features
Windows PowerShell ISE is a full-fledged development environment with a tab-supporting code editor and syntax highlighting, a command builder, an integrated debugger, and other programmer conveniences. If you type a hyphen after the command name in the development environment editor, you will get a dropdown list of all available parameters with their types. You can launch PowerShell ISE either through a shortcut from the system menu or using the executable file powershell_ise.exe.

What is Windows PowerShell and how is it used? Part 1: Key Features

Cmdlets 

In Windows PowerShell, there are so-called cmdlets. These are specialized .NET classes that encompass various functionalities. They are named according to the principle of "Action-Object" (or "Verb-Noun" if you prefer), and the hyphenated connection resembles the predicate and subject in natural language sentences. For example, Get-Help literally means "Get-Help" or, in the context of PowerShell: "Show-Help." Essentially, this is analogous to the man command in Unix systems, and manuals in PowerShell should be requested this way, rather than invoking cmdlets with the --help or /? options. Don't forget about the online PowerShell documentation from Microsoft, which is quite detailed.

In addition to Get, other verbs (and not only verbs, strictly speaking) are used in cmdlets to denote actions. Below is a list with several examples:

Add — add;
Clear — clear;
Enable — enable;
Disable — disable;
New — create;
Remove — remove;
Set — set;
Start — start;
Stop — stop;
Export — export;
Import — import.

There are system, user, and optional cmdlets: as a result of their executions, all of them return an object or an array of objects. They are not case-sensitive, meaning there is no difference in the command interpreter between Get-Help and get-help. The semicolon ‘;’ is used for separation, but you must use it only if multiple cmdlets are executed on the same line. 

Windows PowerShell cmdlets are grouped into modules (NetTCPIP, Hyper-V, etc.), and the cmdlet Get-Command exists for searching by object and action. You can show help for it like this:

Get-Help Get-Command

What is Windows PowerShell and how is it used? Part 1: Key Features

By default, the command displays a brief help, but parameters (arguments) can be passed to cmdlets if necessary. With them, you can, for example, obtain detailed help (parameter -Detailed) or full help (parameter -Full), as well as display examples (parameter -Examples) on the screen:

Get-Help Get-Command -Examples

Help in Windows PowerShell is updated with the cmdlet Update-Help. If the command line becomes too long, cmdlet arguments can be moved to the next line by writing the escape character ‘`’ and pressing Enter — simply finishing the command on one line and continuing on another will not work.

Below are some examples of common cmdlets: 

Get-Process — display the processes running in the system;
Get-Service — display services and their status;
Get-Content — output the content of a file.

For frequently used cmdlets and external utilities in Windows PowerShell, there are short aliases (from the English Alias). For example, dir is an alias for Get-ChildItem. The list of aliases also includes analogs of commands from Unix systems (ls, ps, etc.), and the cmdlet Get-Help is invoked by the command help. A complete list of aliases can be viewed with the cmdlet Get-Alias:

What is Windows PowerShell and how is it used? Part 1: Key Features

Scripts, functions, modules, and the PowerShell language.

Windows PowerShell scripts are stored as plain text files with a .ps1 extension. They cannot be run by double-clicking: you need to right-click to bring up the context menu and select the 'Run with PowerShell' option. From the console, you will either have to specify the full path to the script or navigate to the appropriate directory and type the filename. Running scripts is also restricted by system policy, and to check the current settings, you can use the Get-ExecutionPolicy cmdlet, which will return one of the following values:

Restricted — running scripts is forbidden (by default);
AllSigned — only running scripts signed by a trusted developer is allowed;
RemoteSigned — running signed and local scripts is allowed;
Unrestricted — running any scripts is allowed.

The administrator has two options. The safest approach involves signing scripts, but this is quite complex—we will cover it in upcoming articles. For now, we will take the path of least resistance and change the policy:

Set-ExecutionPolicy RemoteSigned

What is Windows PowerShell and how is it used? Part 1: Key Features
PowerShell will need to be run as an administrator for this, although with a special parameter, you can modify the policy for the current user as well.

Scripts are written in an object-oriented programming language, with commands named in the same way as the previously discussed cmdlets: 'Action-Object' ('Verb-Noun'). Its main purpose is to automate administration tasks, but it is a full-fledged interpreted language that includes all the necessary constructs: conditional statements, loops, variables, arrays, objects, error handling, etc. Any text editor can be used to write scripts, but the most convenient is to launch Windows PowerShell ISE.

You can pass parameters to the script, make them mandatory, and set default values. Additionally, Windows PowerShell allows for creating functions and calling them the same way as cmdlets: this uses the Function construct and curly braces. A script containing functions is called a module and has the .psm1 extension. Modules must be stored in directories defined in the PowerShell environment variables. You can view them using the following command:

Get-ChildItem Env:PSModulePath | Format-Table -AutoSize

Pipelines

In the last example, we applied a familiar construct for Unix shell users. In Windows PowerShell, the vertical bar also allows passing the output of one command to the input of another, but there is a significant difference in the implementation of the pipeline: here we are not just dealing with a set of characters or some text. Built-in cmdlets or custom functions return objects or arrays of objects, and they can also accept them as input. As in the Bourne shell and its many successors, PowerShell simplifies performing complex tasks using the pipeline.

The simplest example of a pipeline looks like this:

Get-Service | Sort-Object -property Status

What is Windows PowerShell and how is it used? Part 1: Key Features
First, the Get-Service cmdlet is executed, and then all obtained services are passed to Sort-Object to be sorted by the Status property. The specific argument to which the result of the previous pipeline segment is passed depends on its type — it is usually InputObject. This issue will be discussed further in the article dedicated to the PowerShell programming language. 

If desired, you can extend the chain and pass the result of Sort-Object to another cmdlet (they will execute from left to right). By the way, users in Windows also have access to the familiar construct for paginated output: 

Get-Service | Sort-Object -property Status | more

Running Background Tasks 

Often, there is a need to run a command in the background so that you do not have to wait for its execution result in the shell session. Windows PowerShell has several cmdlets for such cases:

Start-Job — starts a background job;
Stop-Job — stops a background job;
Get-Job — views the list of background jobs;
Receive-Job — views the result of a background job;
Remove-Job — removes a background job;
Wait-Job — brings a background job back to the console.

To start a background job, we use the Start-Job cmdlet and specify the command or set of commands within curly braces:

Start-Job {Get-Service}

What is Windows PowerShell and how is it used? Part 1: Key Features
You can manipulate background jobs in Windows PowerShell by knowing their names. First, let's learn how to display them:

Get-Job

What is Windows PowerShell and how is it used? Part 1: Key Features
Now let's show the result of Job1:

Receive-Job Job1 | more

What is Windows PowerShell and how is it used? Part 1: Key Features
Everything is quite simple.

Remote Command Execution

Windows PowerShell allows you to run commands and scripts not only on a local computer but also on a remote machine and even on a whole group of machines. There are several ways to do this:

  • Many cmdlets have a parameter -ComputerName, but using this method, it's not possible, for example, to create a pipeline;
  • The cmdlet Enter-PSSession creates an interactive session on a remote machine; 
  • Using the cmdlet Invoke-Command you can execute commands or scripts on one or multiple remote computers.

PowerShell Versions

Since its first release in 2006, PowerShell has undergone significant changes. The tool is available for many systems running on different hardware platforms (x86, x86-64, Itanium, ARM): Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008/2008 R2, Windows 7, Windows 8, Windows 8.1, Windows RT, Windows RT 8.1, Windows Server 2012/2012 R2, Windows 10, Windows Server 2016, GNU/Linux, and OS X. The latest release 6.2 was released on January 10, 2018. Scripts written for earlier versions are likely to work in later versions, but there may be issues with backward compatibility, as many new cmdlets have been introduced over the years. You can find out the version of the command shell installed on your computer using the PSVersion property of the built-in variable $PSVersionTable:

$PSVersionTable.PSVersion

What is Windows PowerShell and how is it used? Part 1: Key Features
You can also use the cmdlet:

Get-Variable -Name PSVersionTable –ValueOnly

What is Windows PowerShell and how is it used? Part 1: Key Features
The same can be done using the Get-Host cmdlet. In fact, there are many options, but to use them, you need to learn the PowerShell programming language, which we will do in the next article. 

Summary 

Microsoft has managed to create a truly powerful command shell with a convenient integrated environment for script development. Unlike the familiar tools from the Unix world, it is deeply integrated with Windows family operating systems, as well as the software for them and the .NET Core platform. PowerShell can be called an object-oriented shell because cmdlets and user functions return objects or arrays of objects and can receive them as input. We believe that all Windows server administrators should master this tool: the time has passed when they could do without the command line. An advanced console shell is particularly necessary on our inexpensive VPS running Windows Server Core, but that's a whole different story.

What is Windows PowerShell and how is it used? Part 1: Key Features

Only registered users can participate in the survey. Please log in, please.

What topics should be addressed first in the upcoming articles of the series?

  • 53,2%Programming in PowerShell123

  • 42,4%PowerShell Functions and Modules98

  • 22,1%How to Sign Your Own Scripts?51

  • 12,1%Working with Repositories Through Providers (providers)28

  • 57,6%Automating Computer Administration Using PowerShell133

  • 30,7%Managing Software and Integrating PowerShell Executables into Third-Party Products71

231 users voted. 37 users abstained.

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster