So you want to learn Powershell

Powershell is a cross-platform functional programming language that is also used for scripting.  It can be used for a wide variety of tasks and the support available for it is very suitable.  You can begin learning it by diving headfirst into the deep end, but you will get more use out of it if you understand a few concepts first. 

The first thing you should familiarize yourself with is the command structure.  It follows a verb-noun pair that makes it easier to work with.  For instance, if you are trying to figure out the name of a command you can list them all with “Get-Command”.  This will list all of the commands available to you. 

While you may see all of the commands, you probably don’t need all of them.  Sometimes you know what you want to do, and this is where understanding the verb in the verb-noun pair comes into play.  A list of the verbs can be found here: https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands?view=powershell-6

Using these verbs you can sort through the Get-Command output using a pipe character: “|” .  The pipe after a command feeds the output of the command into the next script.  So if you issue a command such as “Get-Command | findstr Get” you will see that Powershell applied “findstr” (find string) to the results of Get-Command and it only shows you the commands with Get in them.  This is case sensitive, and there are more ways to manipulate these outputs.

Since we are talking about manipulating the output of commands, now would be a good time to cover some other commands that can be useful with piping.  After a pipe, you can use “less” or “more” to change how the data is presented ie: “Get-Command | less” or “Get-Command | more”.   You can even combine these last two ideas as : “Get-Command | findstr Get | less”.

Sometimes you don’t want to see the output at all.  Instead you can redirect it using “>” and “>>”.  Be careful when using these because they look similar and they can confuse you .  The “>” symbol is used to pipe data to the Success Stream, and it can be used to write to a file in the directory of your choosing.  If no directory is used, then the file will be placed in the directory you are currently in like so: “ls > .\directory.txt”.  This will create a file in your current directory containing a list of the files from your current directory.

If you want to add the files from another directory to your list, you can do so with “>>.”  If you are are trying to append a list of the parent directory to your previous command, you could use: “ls ..\ >> .\directory.txt”.  And here you will find that you have added the parent directory to the end of the file directory.txt.  The “cat” command can then be used to display the file.  If you only want to see the end of the file you could issue: “cat .\directory.txt | tail”.  To help you remember the tail command over using tails, remember you are only using one cat.     

These commands I have gone over are not just for Powershell.  Well, the Get-Command might be, but you will find piping and redirection are used for a lot of different shells.  Understanding how to control what the output of the command is doing is part of stream-lining the process.  I hope this helps you on your Powershell (or any other shell) journey.

Leave a Reply

%d bloggers like this: