Almost all operating systems have their own or integrated graphical interface that allows you to perform complex tasks, understandable even for a novice. However, performing administration tasks, creating automation processes, and generally interacting with the OS is much faster through the CLI. In the Windows family, PowerShell is used to work through the command line interface.
What's PowerShell for?
PowerShell is an object-oriented language and command shell that interprets user commands from a high-level language into a low-level or machine language to work with the OS and create automated scripts. The library of this language contains about one and a half thousand different commands. The main difference from interpreters of nix* systems is work with objects. An object is a certain data structure containing properties and methods for interaction.
This interpreter replaced the well-known cmd.exe, which was clearly limited in its functionality and had not been updated for a long time. Microsoft tried to replace it with a similar solution, Microsoft Script Host, in which JavaScript and VBS were tightly integrated. However, test results showed that this solution was poorly integrated into the OS and instead of it they decided to write their own interpreter from scratch.
As with any interpreter it has two types of commands:
- inbuilt;
- external.
PS or PowerShell accesses them directly or by using environment variables. Built-in commands are a list of pre-prepared operations that interpret user actions in the OS. As well as external commands that have been written by users as utilities or libraries.
How do I start PowerShell?
In Windows, there are two ways to run the interpreter: via search and the Run window. For the first option, go to the desktop and find search at the bottom and type powershell.
The same thing, but using the Win + R key combination, let's type the powershell key.
If you are using a Linux or macOS distribution, a shell is also available for you, которую вы можете which you can download and install according to the instructions..
How do I work with PowerShell?
Like any interpreter, PS has its own syntax and semantics, which define the order and method of command execution. Commandlets are used for operation - they are the same commands, only with a specific syntax, which includes verb-noun options and arguments.
Let's consider the work of the utility on the example of a cloud solution from Serverspace, for this purpose let's proceed to the creation of a VPS server by any of the two platforms Vstack or VMware cloud. Click on the Create Server button and select the configuration that suits our needs, then click Order.
After some time the servers will be available via any of the main connection methods, for the current tasks we have chosen Windows OS with data centre in Istanbul.
Let's consider the syntax of commandlet writing:
Let's analyse the command in detail and explain its operation:
- Get-Help represents a commandlet to address the OS;
- <cmdlet-name> argument accepted as input, in this case the name of another cmdlet;
- -Online option to run the commandlet in the OS.
The concept of a commandlet is absolutely similar to the way commands work in other interpreters, but there is a key difference. If in nix* and other systems the output is a string, in this case it is objects. Commandlets are also case-insensitive and allow the use of different registers.
The first and basic command, for those who are just starting to work with the PS interpreter, is the commandlet-guide, which allows you to search the database for data about commands and examples of their use. Let's imagine that we need to add a new user, but we don't know the command. To do this, let's write:
PS showed a list of commands that mention the word user, in this case we can use the New-LocalUser command. Also information on how to use it is available through the command:
With the help of Get-Help we can find any commands and their description on their use, however, their syntax is rather cumbersome and not always convenient. For this purpose we can use Alias or abbreviated commands:
They can also be used like regular cmdlets, which makes things very easy. Many alias are very similar or analogue to commands from Linux shells, so using them should not be a problem.
Cheat Sheet
There is example how we can use comandlet for implement different tasks:
- Get-Location (pwd): This command outputs the path to the current directory;
- Set-Location (cd): This command changes the current directory;
- Get-ChildItem (ls): This command outputs the contents of the current directory;
- Get-ChildItem find: This command searches for files based on specified criteria;
- Copy-Item (cp): This command copies a file;
- Remove-Item (rm): This command deletes a file;
- New-Item (mkdir): This command creates a directory;
- New-Item (touch): Creates an empty file;
- Get-Content (cat): Outputs files;
- Get-Content (tail): Outputs the last 10 lines;
- Where-Object (grep): Performs filtering;
- Create-Volume Format-Volume (mkfs): Formats the partition;
- Test-Connection (ping): Sends ICMP requests;
- Get-Help (man): Displays help.