Over the last few years, I have been using PowerShell to assist in getting a lot of my day-to-day work done. This is especially more so since the environments IT works in are growing and having scripts do the work is much faster than using the GUI. The frustrating bit is I learn PowerShell based on what I read other IT professionals have done, I modify this for my needs at that time. But when I want to try something new for another task I get errors. These Errors are either Syntax errors or Parsing information that is not understood by the following command-let which in turn takes time to figure out what I am doing wrong.
Thus it is time to spend some time and really gets to learn PowerShell. PowerShell is big and ties into many out-of-the-box applications within Microsoft. It has grown so far as to have 3rd party applications use PowerShell to manage their application\services.
What is Powershell?
PowerShell is a Modern command Shell that includes features from other popular shells. PowerShell is object Oriented meaning that it consists of Class and Object and can be used to simply gather information or configure systems based on a current environment\system state. It allows for bulk automation of management of Systems.
Class and Object
A class is a blueprint that is used when creating an Object. An Object is the specific details you receive back.
A simple example would be, All computers have a Serial Number which it can be identified by. We can call this Class PC_Information. When referring to a specific Computer you would get a Serial Number off 123456. This can be seen as the Object. If the PC_Information Class had a property call Make, the Object would have the same Property but the value would be specific to that Workstation.
Basics of the Language
PowerShell consists of four sections. At its basic level it really only needs two of these Sections the Verb-Noun, combined it is known as a commandlet. It is case-insensitive, you can write it in all caps or all lower case, the output will be the same. PowerShell is a Natural Language meaning it is easily understandable for human reading. The four sections are as follows:
- Verb
- A word that indicates an action or a state of a condition
- Example: Get, New, Set
- A list of Verbs can be found on Microsoft Site: Here
- Required to run a Query
- Noun
- Parameter Name
- Parameter Argument String
- The Value the Parameter will be measured against to get the required Output
- Argument string gets more descriptive. Can use Wildcards, spelling.
The output will give Property Names with Property Values which is not necessarily all the properties and can be expanded on. This can be explored future with the commandlet: Get-Member
Lets run through an Example quickly. If you open PowerShell (In Admin Mode) you can run two queries.
Query 1: Get-Service
This will give you a complete list of current services running on the system you are working on. Get-Service is the bare minimum requirement to start getting information back in PowerShell and consist of only the verb-noun (the Commandlet).
Query 2: Get-Service -DisplayName “*Win*”
This will give you a complete list of current services running on the system where the Display Name contains *Win* in the Property value.
Example cmdlet with all four Sections
This is the bare minimum you need to know to run a query in PowerShell. All commandlets consist of Verb-Noun. You can use it with or without parameters. All outputs are Property Name and Property Value.
Make PowerShell easier
Even though the above mentioned is all you need to run a command in PowerShell, it has taken a step future to make is easier to get more out of the Shell.
Tab-Key
When typing in PS you can simply press the Tab key mid typing and PS will complete the cmdlet for you based on the first avaialble cmdlet that matches the spelling already typed. Notice that even thought PS is Case insensative it will complete the cmdlet in Sentance Case. Which Means it will make it easier to read.
| Pipe Operator |
The Pipe Operator allows you to put the output from one command and pass it onto the next command in the Line.
The output from one command becomes the input for the next command. This allows you to easily pass information onto the next command without having to go through various lines of Script. There is no set limit of how many times the output can be passed on but there is an order in which it is passed on from one command to another.
Aliases
When working with windows Command Prompt, I have a fair amount of commands that I know how it works. It has been imprinted within me over the years. Moving to PS I don’t need to relearn the commands for the shell. I can simply continue to use them. These are set up as Aliases that correspond back to the other shell used in previous versions of other Operating systems from Mac, Windows, and Linux. The best Example would be DIR would have the same output as Get-ChildItem.
Like everything, there are some exceptions to the rule but for the most part, the alias does not give any issues. As Example. If you use the cmdlet Get-Help DIR ti will give you the Helpfile for Get-ChildItem as the alias is set and working fine. But doing the same with get-Help ping will give you a result of various Mapping Commands. Even though ping will still work and you don’t need to use the PS command. There are some commands that the Alias does not correspond to. Note the Ping Equivalent in PS is Test-NetConnection.