Linux and Powershell

## What powershell?

Powershell is an object-based shell born in Microsoft in 2006, made open source 10 years later. It is characterized by commands, called cmdlets, which respect a certain pattern. An example cmdlet is as follows:

Get-ChildItem -Path value


The pattern in question is Verb-Noun -Option argument, where Verb is a list of verbs. Noun is a description of what impacts the command verb. The Option instead are preceded by a dash and its name; to follow, all the arguments of the option.

This pattern guarantees two things:

1. readability: if you read the command, you know what it does. For example, the previous command shows that: I get all the child objects of a given path (files and folders).
2. standard: following such a strict standard, it is possible to introduce various new commands already knowing (thanks to readability) what they do.

## Why powershell on Linux?

Linux has a great scripting tool: bash . Bash is a shell that provides us with a very simple but effective scripting language to write various automatisms. The only problem with bash is that any output is a string and must be treated as such; therefore, we use the same task we took as an example in powershell; we list the files and directories in a given path:

ls -l path


The output produced by this command is a string, so if we need to search for something we have to pass the output as the input of another command, through the pipeline.

ls -l path | grep whatever


Each powershell output is an object. Each object has its own properties that can be used through other cmdlets. For example, the above command will output a System.Array type. This type of object can be used by an iterator (such as the for loop) or by a filter cmdlet, such as Where-Object.

Powershell is an excellent tool for a linux distribution because it offers a series of commands useful for developing tools and automatisms that are very difficult to implement with simple strings.

## Installation on Linux

Depending on your distribution, installing powershell on Linux is a fairly simple task. If you have Debian or Ubuntu, we can install it thanks to snapd , like this:

$sudo apt install snapd$ snap install powershell


Instead on Red Hat based distributions, run the following commands:

$curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo$ sudo yum install -y powershell


To activate powershell, just type pwsh.

### Other distribution

For installation on other distributions refer to the official Microsoft installation page.

## Scripts and modules: automations

One of the advantages of powershell is that you can save commands and various code parts in script and module files.

### Scripts

To create a script file, just write powershell to a file with the .ps1 extension, and parse it to the pwsh interpreter.

# file myscript.ps1

# Get all file modified in the last 3 days
Get-ChildItem -Path path -Recurse | Where-Object {
$_.LastWriteTime -gt (Get-Date).AddDays(-3) }  We have created a script that tells us all files changed in the last 3 days. Fantastic. Now let’s call it from the command line: pwsh myscript.ps1 ### Modules Not all tasks are sequential commands and not all tasks can be contained in a single powershell file. For this, we will use module to collect our functions. Let’s take the script from earlier. Let’s turn it into a function and put it into a module. The module consists of a module file with the extension .psm1 and a module manifest file with the extension .psd1. First, let’s create the module. $ mkdir mymodule
$touch mymodule/mymodule.psm1$ pwsh
PowerShell 7.1.1

https://aka.ms/powershell
Type 'help' to get help.

PS > New-ModuleManifest -Path mymodule/mymodule.psd1 -ModuleVersion "0.0.1" -Author "YourNameHere"


We have created the structure of the module. Now, let’s do a function called Get-LastThreeDaysModifiedFiles containing the above script.

#  mymodule/mymodule.psm1

New-Alias -Name "ls3" Get-LastThreeDaysModifiedFiles

function Get-LastThreeDaysModifiedFiles($Path) { Get-ChildItem -Path$Path -Recurse | Where-Object {
$_.LastWriteTime -gt (Get-Date).AddDays(-3) } }  Now edit our mymodule.psd1 manifest file to add our function and alias for export. Find these two variables and edit them as follows: # mymodule/mymodule.psd1$FunctionsToExport = @('Get-LastThreeDaysModifiedFiles')
\$AliasesToExport = @('ls3')


Now copy our module folder mymodule to the following path (for all users) /usr/local/share/powershell/Modules or ~/.local/share/powershell/Modules (for your user), and open new powershell session:

PS > Get-LastThreeDaysModifiedFiles /your/path/
PS > ls3 /your/path/


## Most used modules

Of course it is possible to install modules from the official repository, for various uses. On a Linux distribution, for example, the PSCouchDB module is useful if you are an administrator of a no-sql database such as CouchDB.

PS > Install-Module -Name PSCouchDB
PS > Get-CouchDBDatabase


This module simplifies the management of the databases and documents that define the characteristic structure of CouchDB. Alternatively, you can use curl but commands for complex tasks will be very long.

## Conclusion

Finally, the goal of powershell in general is to facilitate the end user in scripting simple and complex tasks. Bash is a great tool for many tasks, but powershell can help solve some problems like filtering complex output, thanks to its object-oriented structure. Definitely a great choice for many activities.