Historically, command line utilities in Unix systems are more developed than in Windows, however, with the advent of a new solution, the situation changed.
Windows PowerShell allows system administrators to automate most routine tasks. It can be used to change settings, stop and start services, and maintain most installed applications. Treating the blue window as just another command interpreter would be misleading. This approach doesn't capture the essence of Microsoft's innovations. In reality, the capabilities Windows PowerShell is much broader: in this short series of articles, we'll try to understand how Microsoft's solution differs from the tools we're more familiar with.
Main Features
Of course 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 you to run regular commands and also provides access to COM, WMI, and ADSI objects. It utilizes various storage resources, such as the file system and registry. Windows, which are accessed through so-called providers. It's worth noting the ability to embed PowerShell executable components into other applications to perform various operations, including through a graphical interface. The opposite is also true: many applications for Windows provide access to their management interfaces via 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, registry Windows, certificate storage, etc.
Shell and development environment
Exist Windows PowerShell comes in two forms: in addition to the console emulator and command shell, there's also an Integrated Scripting Environment (ISE). To access the command line interface, simply select the appropriate shortcut from the menu. Windows Or launch powershell.exe from the Run menu. A blue window will appear, noticeably different in capabilities from the ancient cmd.exe. It features autocompletion and other features familiar to users of Unix command shells.

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.

Windows PowerShell ISE is a full-fledged development environment with a tabbed code editor with syntax highlighting, a command designer, a built-in debugger, and other programming delights. If you type a hyphen after a command name in the IDE editor, you'll see a drop-down list of all available parameters, along with their type. You can launch PowerShell ISE either through a shortcut in the system menu or by running the powershell_ise.exe executable.

Cmdlets
Π Windows PowerShell now features so-called cmdlets. These are specialized .NET classes that implement a variety of functionality. They are named using the Action-Object (or Verb-Noun, if you prefer) principle, and the hyphenated pairing 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, it's the equivalent of the man command in Unix systems, and manuals in PowerShell should be accessed this way, rather than by invoking cmdlets with the --help or /? switch. Don't forget about PowerShell's online documentation: Microsoft has quite a detailed one.
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.
Cmdlets Windows PowerShell is grouped into modules (NetTCPIP, Hyper-V, etc.), and the Get-Command cmdlet is used to search for objects and actions. You can display help for it like this:
Get-Help Get-Command

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 using the Update-Help cmdlet. If a command line becomes too long, the cmdlet arguments can be moved to the next line by typing the "`" symbol and pressing Enterβyou can't simply finish typing a command on one line and continue on another.
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 in Windows PowerShell has short synonyms called aliases. For example, dir is an alias for Get-ChildItem. The list of synonyms also includes Unix-like commands (ls, ps, etc.), and the Get-Help cmdlet is invoked by the help command. The full list of synonyms can be viewed using the Get-Alias ββcmdlet:

Scripts, Functions, Modules, and the PowerShell Language
Scripts Windows PowerShell scripts are stored as plain text files with the .ps1 extension. They can't be launched by double-clicking; you must right-click to bring up the context menu and select "Run with PowerShell." From the console, you'll need to either specify the full path to the script or navigate to the appropriate directory and type the file name. Running scripts is also restricted by system policy. 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

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, whose commands are named according to the same principle as the cmdlets discussed earlier: "Action-Object" ("Verb-Noun"). Its main purpose is to automate administrative tasks, but it is a fully-fledged interpreted language that includes all the necessary constructs: conditional branches, loops, variables, arrays, objects, error handling, etc. Any text editor is suitable for writing scripts, but it is most convenient to run 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 just like cmdlets, using the Function construct and curly braces. A script containing functions is called a module and has a .psm1 extension. 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 construction familiar to users of Unix shells. Windows PowerShell's vertical bar also allows you to pipe the output of one command to the input of another, but there's a significant difference in the pipeline's implementation: it's no longer a string of characters or text. Built-in cmdlets and 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 uses the pipeline to simplify complex tasks.
The simplest pipeline example looks like this:
Get-Service | Sort-Object -property Status

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 desired, the chain can be extended and the result of Sort-Object can be passed to another cmdlet (they will be executed from left to right). By the way, users Windows The pagination construction, familiar to all Unix users, is also available:
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}

Background tasks in Windows PowerShell can be manipulated by knowing their names. Let's start by learning how to display them:
Get-Job

Now let's show the result of the job Job1:
Receive-Job Job1 | more

Everything is pretty simple.
Remote command execution
Windows PowerShell allows you to run commands and scripts not only on the local computer, but also on remote computers and even across entire groups 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-PSSessionallows you to create an interactive session on a remote machine; - Using the cmdlet
Invoke-Commandyou can run commands or scripts on one or more remote computers.
Versions of PowerShell
PowerShell has evolved significantly since its initial release in 2006. It is available for a wide range of systems running on different hardware platforms (x86, x86-64, Itanium, ARM): Windows XP, Windows Server 2003 Windows Vista, Windows Server 2008/2008R2, Windows 7, Windows 8, Windows 8.1 Windows RT, Windows RT 8.1, Windows Server 2012/2012R2, 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 backporting may be problematic, as PowerShell has added a large number of new cmdlets 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 $PSVersionTable 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 we will do in .
Conclusion
Microsoft has created a truly powerful command shell with a convenient integrated environment for script development. What sets it apart from the tools we're accustomed to in the Unix world is its deep integration with operating systems of the family. Windows, as well as the 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 as input. We believe this tool is a must-have for all server administrators. Windows: the time has passed when they could do without the command line. An advanced console shell is especially necessary on , but that's a completely different story.
Only registered users can participate in the survey. , 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
