News
Seven days of Black Friday madness — great deal from Serverspace!

THE NEW YEAR
WHEEL OF FORTUNE

Spin the wheel and win a guaranteed prize right now!

By registering, you are signing up to receiving emails.
RF
March 6 2024
Updated March 1 2024

PowerShell commands Cheat Sheet

AD Windows

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.

First option for open PS
Screenshot №1 — First option for open PS

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

Second option for open PS
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.

Create machine
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:

  • 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:

Get-Help user
Search needed command
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
Help page of utility
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
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:

  • 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.
Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.