What is Windows PowerShell and what is it eaten with? Part 1: Key Features

Historically, command-line utilities on Unix systems are better developed than on Windows, but with the advent of a new solution, the situation has changed.

Windows PowerShell allows system administrators to automate most routine tasks. With it, you can change settings, stop and start services, and also perform maintenance on most installed applications. It would be wrong to perceive the blue window as another command interpreter. This approach does not reflect the essence of the innovations proposed by Microsoft. In fact, the possibilities of Windows PowerShell are much wider: in a short series of articles, we will try to figure out how the Microsoft solution differs from the tools we are more familiar with.

What is Windows PowerShell and what is it eaten with? Part 1: Key Features

Main Features 

Of course, Windows PowerShell is primarily a scripting shell, 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 that have properties and methods. PowerShell allows you to run common commands and also gives you access to COM, WMI, and ADSI objects. It uses various storages, such as the file system or the Windows registry, for access to which so-called. providers. It is worth noting the possibility of embedding PowerShell executable components in other applications to implement various operations, incl. through a graphical interface. The reverse is also true: many Windows applications provide access to their management interfaces through PowerShell. 

Windows PowerShell allows you to:

  • Change operating system settings;
  • Manage services and processes;
  • Configure server roles and components;
  • Install software;
  • Manage installed software through special interfaces;
  • Embed executable components in third-party programs;
  • Create scripts to automate administration tasks;
  • 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 appropriate shortcut from the Windows menu or run powershell.exe from the Run menu. A blue window will appear on the screen, noticeably different in capabilities from the antediluvian cmd.exe. There is autocompletion and other features familiar to users of command shells for Unix systems.

What is Windows PowerShell and what is it eaten with? 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 typed commands;
  • The right arrow at the end of a line retypes the previous command character by character;
  • Ctrl+Home deletes the typed 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 shows a window with typed commands and allows you to select one of them. The console also works with text selection with the mouse, copy-paste, cursor positioning, deletion, backspace - everything we like.

What is Windows PowerShell and what is it eaten with? Part 1: Key Features
Windows PowerShell ISE is a complete development environment with a tabbed and syntax-highlighted code editor, command builder, built-in debugger, and other programming pleasures. If you write a hyphen after the command name in the development environment editor, you will get all available options in the drop-down list with an indication of the type. 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 what is it eaten with? Part 1: Key Features

Cmdlets 

In Windows PowerShell, the so-called. cmdlets. These are specialized .NET classes that provide a variety of functionality. They're named Action-Object (or Verb-Noun, if you prefer), and the hyphen-separated link resembles the predicate and subject in natural language sentences. For example, Get-Help literally means "Get-Help", or in a PowerShell context: "Show-Help". In fact, this is an analogue of the man command in Unix systems, and manuals in PowerShell must be requested in this way, and not by calling cmdlets with the --help or /? key. Do not forget about the online PowerShell documentation: Microsoft has it quite detailed.

In addition to Get, cmdlets use other verbs to denote actions (and not only verbs, strictly speaking). In the list below we give some examples:

Add - add;
Clear - clear;
Enable - turn on;
Disable - switch off;
New - create;
Remove - delete;
Set - ask;
Start - run;
Stop - stop;
Export - export;
Import - import.

There are system, user and optional cmdlets: as a result of execution, they all return an object or an array of objects. They are not case sensitive, i.e. from the command interpreter's point of view, there is no difference between Get-Help and get-help. The character ';' is used for separation, but it is mandatory to put it only if several cmdlets are executed on the same line. 

Windows PowerShell cmdlets are grouped into modules (NetTCPIP, Hyper-V, etc.), and there is a Get-Command cmdlet to search by object and action. You can display help for it like this:

Get-Help Get-Command

What is Windows PowerShell and what is it eaten with? Part 1: Key Features

By default, the command displays brief help, but parameters (arguments) are passed to cmdlets as needed. With their help, you can, for example, get detailed (parameter -Detailed) or full (parameter -Full) help, as well as display examples (parameter -Examples):

Get-Help Get-Command -Examples

Help in Windows PowerShell is updated by the Update-Help cmdlet. If the command line turns out to be too long, the cmdlet arguments can be moved to the next one by writing the service character '`' and pressing Enter - just finishing writing the command on one line and continuing on another will not work.

Here are some examples of common cmdlets: 

Get-Process - show running processes in the system;
Get-Service β€” show services and their status;
Get-Content - display the contents of the file.

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

What is Windows PowerShell and what is it eaten with? Part 1: Key Features

Scripts, Functions, Modules, and the PowerShell Language

Windows PowerShell scripts are stored as plain text files with a .ps1 extension. You cannot launch them by double-clicking: you need to right-click to call up the context menu and select the β€œRun in PowerShell” item. From the console, you will either have to specify the full path to the script, or go to the appropriate directory and write the file name. Running scripts is also limited 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 β€” launching scripts is disabled (by default);
AllSigned - only the launch of scripts signed by a trusted developer is allowed;
RemoteSigned - allowed to run signed and own scripts;
Unrestricted - allowed to run any scripts.

The administrator has two options. The most secure involves signing scripts, but this is quite a serious sorcery - we will deal with it in future articles. Now let's take the path of least resistance and change the policy:

Set-ExecutionPolicy RemoteSigned

What is Windows PowerShell and what is it eaten with? Part 1: Key Features
PowerShell will need to be run as an administrator to do this, although you can change the policy for the current user with a special setting.

Scripts are written in an object-oriented programming language, the commands of which are named according to the same principle as the previously discussed cmdlets: "Action-Object" ("Verb-Noun"). Its main purpose is to automate administrative tasks, but it is a full-fledged interpreted language that has all the necessary constructs: conditional jump, loops, variables, arrays, objects, error handling, etc. Any text editor is fine for scripting, but it's best to run Windows PowerShell ISE.

You can pass parameters to the script, make them required, and set default values. In addition, Windows PowerShell allows you to create and call functions in the same way as cmdlets, by using the Function construct and curly braces. A script with functions is called a module and has a .psm1 extension. Modules must be stored in directories defined in the PowerShell environment variables. You can view them with the following command:

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

Conveyors

In the last example, we have used a construct that is familiar to Unix shell users. In Windows PowerShell, the vertical bar also allows you to pass the output of one command to the input of another, but there is a significant difference in the implementation of the pipeline: we are no longer talking about a set of characters or some kind of text. Built-in cmdlets or user-defined functions return objects or arrays of objects, and can also receive them as input. Like the Bourne shell and its many successors, PowerShell makes complex tasks easier with a pipeline.

The simplest pipeline example looks like this:

Get-Service | Sort-Object -property Status

What is Windows PowerShell and what is it eaten with? Part 1: Key Features
First, the Get-Service cmdlet is executed, and then all the services received by it are passed to the Sort-Object cmdlet for sorting by the Status property. Which argument the result of the previous section of the pipeline is passed to depends on its type - usually it is InputObject. This issue will be discussed in more detail in an article dedicated to the PowerShell programming language. 

If you wish, you can continue the chain and pass the result of the Sort-Object operation to another cmdlet (they will be executed from left to right). By the way, Windows users also have access to the construction for pagination familiar to all Unixoids: 

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

Running tasks in the background 

Quite often, it is necessary to run a certain command in the background, so as not to wait for the result of its execution in the shell session. Windows PowerShell has several cmdlets for this case:

Start-Job - launching a background task;
Stop-Job β€” stop the background task;
Get-Job β€” view the list of background tasks;
Receive-Job β€” viewing the result of the background task execution;
Remove-Job β€” deleting a background task;
Wait-Job - transferring the background task back to the console.

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

Start-Job {Get-Service}

What is Windows PowerShell and what is it eaten with? Part 1: Key Features
Background tasks in Windows PowerShell can be manipulated by knowing their names. First, let's learn how to display them:

Get-Job

What is Windows PowerShell and what is it eaten with? Part 1: Key Features
Now let's show the result of the job Job1:

Receive-Job Job1 | more

What is Windows PowerShell and what is it eaten with? Part 1: Key Features
Everything is pretty simple.

Remote command execution

Windows PowerShell allows you to execute commands and scripts not only on the local computer, but also on a remote computer, and even on an entire group of machines. There are several ways to do this:

  • Many cmdlets have a parameter -ComputerName, but in this way it will not work, for example, to create a conveyor;
  • Cmdlet Enter-PSSession allows you to create an interactive session on a remote machine; 
  • Using the cmdlet Invoke-Command you can run commands or scripts on one or more remote computers.

Versions of PowerShell

PowerShell has changed a lot since its first release in 2006. 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. Latest release 6.2 released on January 10, 2018. Scripts written for earlier versions are more likely to work in later versions, but backporting can be problematic because PowerShell has introduced a large number of new cmdlets over the years of development. You can find out the version of the command shell installed on the computer using the PSVersion property of the $PSVersionTable built-in variable:

$PSVersionTable.PSVersion

What is Windows PowerShell and what is it eaten with? Part 1: Key Features
You can also use the cmdlet:

Get-Variable -Name PSVersionTable –ValueOnly

What is Windows PowerShell and what is it eaten with? Part 1: Key Features
The same is done with 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 next article

Results 

Microsoft has managed to create a really powerful shell with a convenient integrated environment for developing scripts. It differs from the tools familiar to us in the world of Unix by deep integration with operating systems of the Windows family, as well as with software for them and the .NET Core platform. PowerShell can be called an object-oriented shell because cmdlets and user-defined functions return objects or arrays of objects and can take them as input. We think that all server administrators on Windows should own this tool: the time has passed when they could do without the command line. An advanced console shell is especially needed on our low cost VPS running Windows Server Core, but that's a completely different story.

What is Windows PowerShell and what is it eaten with? Part 1: Key Features

Only registered users can participate in the survey. Sign in, you are welcome.

What topics should be covered first in the next articles in the series?

  • 53,2%Programming in PowerShell123

  • 42,4%PowerShell98 Functions and Modules

  • 22,1%How to sign your own scripts?51

  • 12,1%Working with repositories through providers (providers)28

  • 57,6%Automating Computer Administration with PowerShell133

  • 30,7%Software management and embedding PowerShell executables in third-party products71

231 users voted. 37 users abstained.

Source: habr.com

Add a comment