What is Windows PowerShell and what does it eat? Part 1: Key Features

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

Windows PowerShell allows system administrators to automate most routine tasks. With its help, you can change settings, stop and start services, as well as perform maintenance of 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. Actually, 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 more familiar tools.



Key features 


Of course, Windows PowerShell is primarily a command shell with a scripting language, originally created on the basis of the .NET Framework, and later on the .NET Core. Unlike shells that accept and return textual data, Windows PowerShell works with .NET classes that have properties and methods. PowerShell allows you to execute ordinary commands, and also gives access to COM, WMI, and ADSI objects. It uses various repositories, such as the file system or the Windows registry, for access to which the so-called suppliers (providers). It is worth noting the ability to embed executable PowerShell components in other applications to implement various operations, including through the graphical interface. The converse is also true: many Windows applications provide access to their management interfaces through PowerShell. 

Windows PowerShell allows you to:

  • Change the 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


There is Windows PowerShell in two ways: in addition to the console emulator with a command shell, there is an integrated scripting environment (ISE). To access the command line interface, just select the appropriate shortcut in the Windows menu or run powershell.exe from the Run menu. A blue window appears on the screen, significantly different in capabilities from the antediluvian cmd.exe. There is autocompletion and other features familiar to users of command shells for Unix systems.



To work with the shell, you need to remember some key combinations:

  • Up and down arrows scroll through history to repeat previously typed commands;
  • The right arrow at the end of the line recycles 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 the text from the cursor to the end of the line.

F7 displays a window with the typed commands and allows you to select one of them. The mouse selection, copy-paste, cursor positioning, deletion, backspace also work in the console - all as we like.


Windows PowerShell ISE is a full-fledged development environment that supports tabs and syntax highlighting with a code editor, command designer, built-in debugger, and other programming joys. If you write a hyphen in the development environment editor after the command name, you will get all available parameters with a type in the drop-down list. You can launch PowerShell ISE either through a shortcut from the system menu or using the powershell_ise.exe executable file.


Cmdlets 


In Windows PowerShell, the so-called cmdlets These are specialized .NET classes that contain diverse functionality. They are named according to the principle "Action-Object" (or "Verb-Noun, if you like it more), and a hyphen-separated connective resembles the predicate and subject in sentences of natural languages. For example, Get-Help literally means “Get-Help” or, in the context of PowerShell: “Show-Help”. In fact, this is an analogue of the man command in Unix systems and the manuals in PowerShell need to 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, other verbs (and not only verbs, strictly speaking) are used in cmdlets to indicate actions. In the list below we give a few examples:

Add- add;
Clear- clean;
Enable- enable;
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 point of view of the command interpreter, there is no difference between Get-Help and get-help. The ';' character is used for separation, but it is mandatory to set it only if several cmdlets are executed on the same line. 

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

Get-Help Get-Command



By default, the command displays a brief help, but if necessary, parameters (arguments) are passed to the cmdlets. Using them, for example, you can get detailed (-Detailed parameter) or full (-Full parameter) help, and also display examples (-Examples parameter):

Get-Help Get-Command -Examples

Help in Windows PowerShell is updated with the Update-Help cmdlet. If the command line is too long, the arguments of the cmdlet can be transferred to the next by writing the service character `` '' and pressing Enter - just finish writing the command on one line and continue on another.

Below 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 in Windows PowerShell there are short synonyms - aliases (from the English Alias). For example, dir is an alias of Get-ChildItem. The list of synonyms contains 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:


PowerShell Scripts, Functions, Modules, and Language


Windows PowerShell scripts are stored as plain text files with the .ps1 extension. You cannot launch them with a double click: you need to right-click 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 the system policy, and to check the current settings, you can use the Get-ExecutionPolicy cmdlet, which will give one of the following values:

Restricted- script execution is prohibited (by default);
AllSigned- only the launch of scripts signed by a trusted developer is allowed;
RemoteSigned- allowed to run signed and custom scripts;
Unrestricted- allowed to run any scripts.

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

Set-ExecutionPolicy RemoteSigned


To do this, PowerShell will have to be run as administrator, although using a special parameter you can change the policy for the current user.

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 automation of administration tasks, but it is a full-fledged interpreted language that has all the necessary constructions: conditional branching, loops, variables, arrays, objects, error handling, etc. Any text editor is suitable for writing scripts, but it is most convenient to start Windows PowerShell ISE.

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

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

Conveyors


In the last example, we used a design familiar to users of shells for Unix systems. In Windows PowerShell, the vertical bar also allows you to transfer the output of one command to the input of another, but there is a significant difference in the implementation of the pipeline: this is not about a character set or some text. Built-in cmdlets or user-defined functions return objects or arrays of objects, and can also receive them at the input. Like the Bourne shell and its many followers, PowerShell simplifies complex tasks with a pipeline.

The simplest example of a pipeline looks like this:

Get-Service | Sort-Object -property Status


First, the Get-Service cmdlet is run, and then all the services it receives 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 this is an InputObject. This issue will be discussed in more detail in the article devoted to the PowerShell programming language. 

If you wish, you can continue the chain and pass the result of Sort-Object to another cmdlet (they will be executed from left to right). By the way, the Windows-familiar design for paging output is also available for all Unixoids: 

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

Running tasks in the background 


Quite often, you need to run a certain command in the background so as not to wait for the result of its execution in a shell session. In Windows PowerShell, there are several cmdlets for this case:

Start-Job- launching a background task;
Stop-Job- stop the background task;
Get-Job- View a list of background tasks;
Receive-Job- view the result of the background task;
Remove-Job- removal of the background task;
Wait-Job- Transfer the background task back to the console.

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

Start-Job {Get-Service}


Background tasks in Windows PowerShell can be manipulated by knowing their names. First, learn how to display them:

Get-Job


Now we show the result of Job1:

Receive-Job Job1 | more


It's pretty simple.

Remote command execution


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

  • Many cmdlets have a parameter -ComputerName, but this way will fail, for example, creating a pipeline;
  • The cmdlet Enter-PSSessionallows you to create an interactive session on a remote machine; 
  • Using the cmdlet, Invoke-Commandyou can execute commands or scripts on one or more remote computers.

PowerShell Versions


Since its first release in 2006, PowerShell has changed a lot. 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 more likely to work in later versions, but there may be problems with reverse porting, because over the years of development, a large number of new cmdlets have appeared in PowerShell. You can find out the version of the shell installed on the computer using the PSVersion property of the $ PSVersionTable built-in variable:

$PSVersionTable.PSVersion


You can also use the cmdlet:

Get-Variable -Name PSVersionTable –ValueOnly


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 is what we will do in the next article

Summary 


Microsoft has managed to create a truly powerful command shell with a convenient integrated environment for developing scripts. It differs from the means familiar to us in the Unix world by deep integration with Windows operating systems, 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 receive them at the input. We think that all Windows server administrators 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 another story.

2: Windows PowerShell
3: ,
4: ,

Source: https://habr.com/ru/post/undefined/


All Articles