PowerShell 101: an alternative to command line on Linux and Mac

The Command Line Interface (CLI) in Windows® exists and resists the passage of time, from those distant days of MS-DOS® to the current PowerShell 101. Let’s see the basic PowerShell.

When I went to college in the 1980s, proprietary software reigned. The old German computers with Unix® that printed our schedules were being replaced by “modern” personal computers. The Microsoft® software house – at that time – was tied to MS-DOS®, so we learned to use the commands: dir, cls, format for our floppy disks…

A little over a decade ago, back in Redmond they decided to dust off, modernize and empower the CLI. Born as Monad®, renamed PowerShell®, today we present you with the basic PowerShell or PowerShell 101.

powershell 101 1

Description: Basic Powershell – Logo
(Wikipedia https://commons.wikimedia.org/wiki/File:PowerShell_5.0_icon.png)

Basic PowerShell

The Linux operating system has been going on strong; there are many articles in Pandora FMS blog about it. Also in the monitoring of proprietary systems we are always present; Windows®, as its maximum exponent. And every time a new version comes out we’re there, testing and checking.

I think, because of this Linux thing, Microsoft decided to make a compilation of tools along with new concepts, as a counterpart to the GNU features that come with Linux. Considering that PowerShell Core exists since 2016 as open software (MIT license) but with Windows® proprietary components, now we have it available in Ubuntu, CentOS (the OS recommended for Pandora FMS) and macOS and even in another hardware architecture such as ARM.

Installing PowerShell Core at Linux

In Ubuntu we must install the package manager snap: with sudo apt install snap we will achieve our mission. Next we’ll run snap install powershell -classic

powershell 101 2

Description: snap install powershell – classic

First commands

Having launched with the command pwsh (in Windows® we should look for powershell.exe), we will have a terminal window, with “PS” of indicative (prompt) followed by the location of the directory. In both environments the aspect is very similar, so we will generalize from now on.

Then let’s put our memory into practice:

  • cls: “clears” the screen, leaving space to execute a new cycle of commands. It’s not necessary at all, but it’s similar to writing with chalk on a blackboard and erasing to begin to explain another subject.
  • dir -ad: to list directories only.
  • echo message: when we want to show specific text on the screen. This doesn’t seem to be useful, but when we integrate it in a script it is of tremendous utility to indicate the progress of some task or the result of the same one.

We won’t delay any longer with the old MS-DOS commands. In the twenty-first century, we would need to continue using such old technology, and in the process with those old programs that communicated or interacted with text strings (STDIN, STDOUT).

Basic Command-let in PowerShell 101

In the 21st century everything is more complex, they are years of accumulated experience. PowerShell 101 is not a simple tool like the one we use in Linux. For this tool there are command-let and its name is abbreviated as cmdlet. This means that the commands we tested are not really the ones we thought they were: they are aliases of the default cmdlet and this allows backward compatibility. Now, there’s more. Let’s analyse the case of the command date, used to remunerate the date.

Its real name is Get-Date and although it returns in a slightly different format the current date and time to that of the alias, basically both do the same thing. For monitoring tasks we need to deliver that value in a very specific format: this is when the cmdlets do their job in a totally different way.

powershell 101 3

Description: «Working with cmdlet with date and time variables»

With the cmdlet Get-Date we can:

  • Display the date of the computer.
  • Display it in a custom format.
  • Use methods; in this case we visualize what day number is the date May 20, 2019 (it is the 140th day of the year).
  • Save a date variable in a custom format.
  • Convert this variable to a text string and save it in a file.
  • Notice that we have used the pipe to communicate one cmdlet with another. The cmdlet you receive used to write to disk is called Add-Content. (Don’t you remember Linux?).
  • The reading counterpart is Get-Content and its alias is called… “cat”, just like the one used in Linux to list the contents of a text file!

Take a pause, check this before moving on to the next point.

Working with cmdlet

With all this as a base, we can stop thinking of basic Powershell as a tool and start evoking it as a toolbox. To do this we will use the Get-Command command:

Using it without any parameter will give us back a lot of tools; the ones we have installed in our computer.

If we inquire about a particular command, for example Get-Command Get-Date will return information about the command type, name, version and source (the library it belongs to). For Get-Date it will indicate that it is a cmdlet belonging to Microsoft.PowerShell.Utility and for Clear-Host (clear screen, cls) that it is simply a function. Entering an alias will return the original cmdlet or function.

If we don’t remember the name exactly we’ll use wildcards; for example, with Get-Command *date* we’ll get a list of all the commands that contain that string.

Help with basic Powershell

The help was also thought as a repository, since with the Get-Help command we can also download content to our computer:

  • Get-Help Get-Date: will show complete information about how to use Get-Date, its syntax, its aliases, etc.
  • Get-Help Get-Date -Online: will open an instance of our web browser and open the latest online information about the Get-Date command.
    To work offline, i.e. to save the updated help: Update-Help.

Using Get-Help, let’s learn about the commands Get-Location and Set-Location.

Let’s suppose we have to create a folder or a file; for this we will no longer use the command md or mkdir (the latter is written exactly the same in Linux) but we will use the New-Item command:

New-Item “path/name” -type directory

New-item Command.

“path/name” of the directory; quotation marks are required when interspersed.

Parameter -type and then what we have installed as provider: File, Directory, SymbolicLink, Junction or HardLink

Now let’s talk about providers: we can download the providers we need or we can create our own providers and associate them to the command. My imagination flies: we develop a program that acts as an FTP client and we offer it as a provider so we can sell it to anyone to integrate it into their PowerShell… but wait, there is more. If we do this in turn -if our license allows it- our client can add our FTP program as a library to their own projects. What do you think?

Note: PowerShell is also able to work via API and even security analysts have created their own PowerShell environments, some mixed with Python language… who don’t even need Microsoft executable files!

Pandora FMS and monitoring tasks

Pandora FMS flexibility allows us to use PowerShell to quickly access complex commands. For example, in Windows environment we need to know which patches are installed:

Get-CimInstance -ClassName Win32_QuickFixEngineering -ComputerName

We will be able to visualize the components with the Get-Member command, extract the contents and make our complement in Pandora FMS for PowerShell!

Do you want to know better what Pandora FMS can offer you? Find out here.

If you have more than 100 devices to monitor you can contact the Pandora FMS team through the following form.

Also, remember that if your monitoring needs are more limited you have at your disposal the OpenSource version of Pandora FMS. Find more information here.

Shares