PowerShell – an opinionated primer

PowerShell

One day I discovered that there was a Windows App called “PowerShell” while searching on my Windows 10 installation. I did not pay much attention to it even if I stumbled upon it a few times since. It seems that the early versions were present already in Windows 7, so in that sense it is not new at all. In fact it started in 2002.

But now I got curious and even more when just finding out that PowerShell is also available for Mac and Linux. If you are curious as well here are a few useful, selected links:

Learning PowerShell

3 easy videos

These 3 videos are a good way to see PowerShell in action.

PowerShell format

The format of all commands is a set of 2 words connected with a dash: the first word is always a verb (hence some ‘action’) and the second word is a a noun (the object of the action.)

Two useful commands therefore are:

  • Get-Command: provides a list of all commands available
  • Get-Verb: provides a shorter list of about 100 verbs that are used to construct commands.

While PowerShell is available on all 3 major platforms, the command Get-Service related to “services” (what programs are running) and all commands “*-Service” are only available for Windows.

PowerShell vs bash

The above video “15 Useful PowerShell Commands for Beginners | Learn Microsoft PowerShell” convinced me that for now I’ll stick with bash as I find PowerShell extremely “wordy.” For example let’s look at the command to move a file:

PowerShell example from the video

Move-Item –Path c:\test\testfile2.txt –Destination c:\Test3

Equivalent bash version:

mv /test/testfile2.txt /Test3

It also seems from searching the web that PowerShell is most useful for IT managers and Windows users that have use of writing (automated maintenance) scripts.

Alternate use: PowerShell as a Terminal

The above links are useful to learn the PowerShell language, which, it seems, is most useful for IT administrators than casual users.

On the other hand, the PowerShell App included with current Windows (10) installations can be a good candidate to be used as a “dumb terminal” as they used to be called. Indeed the PowerShell App works well to connect to a remote (Linux) computer such as the Biochemistry Compute Cluster (BCC) or the UW-Madison Center for High Throughput Computing (CHTC) using the ssh command (secure shell remote connection.)

In that capacity the PowerShell App is easier to use than the old cmd application even for simply copy/paste of text.

PowerShell or cmd

Quoting from this Blog: Windows PowerShell vs Command Prompt (cmd): Which to Choose:

…if you’re used to using cmd, you don’t need to feel any urgency in switching to PowerShell. In fact, most commands from cmd work fine in the PowerShell environment — Microsoft wanted to simplify the lives of system administrators, so it created command prompt aliases in PowerShell that enable it to interpret old DOS commands as new PowerShell commands.

o find out how old cmd commands map to the newer PowerShell cmdlets, use the Get-Alias command:

Get-Alias cd, dir, ls, man

CommandType Name
----------- ----
Alias cd -> Set-Location
Alias dir -> Get-ChildItem
Alias ls -> Get-ChildItem
Alias man -> help

(Note: On MacOS it complains that there is no alias for both ls and manwhich are both native commands on these systems! Therefore there is big doubt in my mind that the cross-platform claim is truly operational!)


Note: DOS = Disk Operating System


Image Credit: PowerShell screen pasted onWindows PC illustration.