01.03.2024

PowerShell commands Cheat Sheet

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:

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.

Screenshot №1 — First option for open PS

The same thing, but using the Win + R key combination, let's type the powershell key.

Screenshot №2 — Second option for open PS

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.

Screenshot №3 — Create machine

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:

Get-Help <cmdlet-name> -Online

Let's analyse the command in detail and explain its operation:

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:

Get-Help user

Screenshot №4 — Search needed command

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:

Get-Help  New-LocalUser

Screenshot №5 — Help page of utility

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:

Get-Alias

Screenshot №6 — Get- Alias

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: