Loading...
X

Linux PowerShell Basics (Beginner’s Guide)

PowerShell for Linux

In recent years, there has been a trend at Microsoft to cross-platform some of its products and open their source. Also Linux itself became part of Windows in the form of a subsystem. PowerShell, which was originally a Windows-only component, was released as open source and became cross-platform on August 18, 2016, available for Linux and Mac OS.

PowerShell is a task automation and configuration management system developed by Microsoft. It consists of a command interpreter (shell) and a scripting language built on the .NET Framework.

It offers full access to COM (Component Object Model) and WMI (Windows Management Instrumentation), thereby allowing system administrators to perform administrative tasks on both local and remote Windows systems, as well as WS-Management and CIM (Common Information Model), allowing you to administer remote Linux systems plus network devices.

Within this framework, administrative tasks are mostly performed by specific .NET classes called cmdlets (pronounced command-let). Similar to shell scripts in Linux, users can create scripts or executables by storing groups of cmdlets in files following specific rules. These scripts can be used as standalone utilities or command line tools.

How to install PowerShell on Linux

Installation on various Linux distributions is described in the official documentation “Installing PowerShell on Linux”.

But in the official documentation there is a lack of information about some distributions, to fill this gap, separate instructions were written:

Features of PowerShell on Linux

1. Command case is not important in PowerShell

In Windows, the case of commands and names of files and folders is not important – this is a big difference from Linux, where the case of both commands and files matters.

PowerShell inherits a feature from Windows – case is not important. Although this tutorial shows commands written in different case (for example, Get-Date), this is actually for better readability and it doesn't matter if you enter Get-Date, GET-DATE, get-date, or gET- dATE.

2. Some commands are missing

The set of commands in PowerShell depends on the available modules, that is, even on a Windows computer, if you install, for example, Active Directory, then new commands will appear in PowerShell.

The Linux version of PowerShell lacks some of the commands found in the standard Windows PowerShell, primarily when it comes to Windows-specific things.

3. Many aliases and abbreviations

You can see in the scripts and examples of PowerShell commands the unusual writing of commands – in PowerShell, abbreviated notation is allowed, and there are also many aliases, that is, the same action can be performed by commands written in different ways.

4. PowerShell versions

Please note that PowerShell 5 is currently preinstalled on Windows by default, and this shows the installation of the latest version of PowerShell 7. On Windows, you can also install PowerShell 7, but this version will not replace the preinstalled version – there will be 2 versions of PowerShell, moreover, when launched from the “Power user menu” (Win+x) will open PowerShell 5, and when you start Windows Terminal, PowerShell 7 will open.

5. Skipping option names

Some options are positional and when specifying their values, you can skip the option names – at first this may seem confusing.

How to use Powershell on Linux

In this section, we will have a short introduction to Powershell; where we will see how to start PowerShell, run some basic commands, see how to work with files, directories and processes. Later, you will learn how to list all available commands, show command help and aliases.

To start Powershell, enter:

pwsh

You can check the Powershell version with the following command:

$PSVersionTable

If you only need the version number, then use the following construction:

Get-Host | Select-Object Version

Running some basic Powershell commands on Linux.

Show current date:

Get-Date

Show computer uptime:

Get-Uptime

Show current working directory:

Get-Location

Working with files and directories in Powershell

1. Create a new empty file in two ways:

New-Item hackware.stuff

Or:

"">hackware.stuff

Then add content to it and view the content of the file.

Set-Content hackware.stuff -value "Делаем pwsh.ru - это весело!"
Get-Content hackware.stuff

2. Delete the file in PowerShell.

Remove-Item hackware.stuff
Get-Content hackware.stuff

3. Create a new directory.

Take a look at the following set of commands:

mkdir hackware-files
cd hackware-files
"">domains.list
ls

Sound familiar? Yes, as already mentioned, there are many aliases in PowerShell. The same actions can be performed with the following commands:

New-Item hackware-files -ItemType "directory"
Set-Location hackware-files
New-Item domains.list
Get-ChildItem -Name

In fact, the commands don't just do the same thing – they are identical.

4. To display a long list that displays detailed information about files and directories, including mode (file type), last modified time, enter:

dir

Or:

Get-ChildItem

To display the contents of a specific folder (in this case, the root of the file system), run:

Get-ChildItem /

Working with Processes in PowerShell

How to view all running processes in Linux using PowerShell:

Get-Process

To view detailed information about one or a group of running processes with a given name, specify the process name as an argument to the previous command as follows:

Get-Process httpd

The value of the units in the output is above:

  • NPM(K) – amount of non-paged memory that the process is using, in kilobytes.
  • PM(K) – amount of pageable memory that the process is using, in kilobytes.
  • WS(K) – size of the working set of the process, in kilobytes. The working set consists of the pages of memory that were recently referenced by the process.
  • CPU(s) – amount of processor time that the process has used on all processors, in seconds.
  • ID – process ID (PID).
  • ProcessName – name of the process.

To get all possible data about one or more processes use a command of the form (replace httpd with the name of the process you are interested in):

Get-Process httpd | Format-List *

To stop the process with ID 10500 use the command as shown below:

Get-Process -Id 10500 | Stop-Process

Getting Help in PowerShell

To learn more, get a list of all PowerShell commands for various tasks:

Get-Command

It is possible to filter the information displayed by the Get-Command command. Let's say you want to see PowerShell commands containing the word “Alias”, for this you need to run the following command:

Get-Command -Name *Alias

To display help about a command (cmdlet) use the following:

Get-Help CMDLETS

For example, to display help about the Get-Alias cmdlet:

Get-Help Get-Alias

To get the most complete help on Get-Command, do the following:

Get-Help Get-Command -Full

Aliases in PowerShell

To see all available command aliases, enter:

Get-Alias

To get information about a specific alias, enter its name:

Get-Alias -Name pwd

PowerShell Command History

Last but not least, display the command history (a list of the commands you ran earlier) like this:

history

So this was an introduction to PowerShell on Linux. Of course, PowerShell can actually do much more than create files and show running processes.


Leave Your Observation

Your email address will not be published. Required fields are marked *