PowerShell Basics: What is PowerShell?

Learn the Basics of PowerShell Scripting Language and Commands

PowerShell Basics: What is PowerShell?

The system administrator has always been striving for automation. However, for an IT person, repeating the same tasks can become monotonous. Not only does it reduces efficiency, but it can also lead to human errors.

To address the issue of building effective scripts and tools, PowerShell was built using .Net runtime. Due to the flexibility and the powerful command-line execution speed with support for multiple services, PowerShell is loved in the sysadmin and IT world to automate the repeating mundane tasks.

In this article, you'll discover the basics of PowerShell and what you can do with it.

Table of Content

  1. What is PowerShell?
  2. What are PowerShell Commands and Cmdlets
  3. What are PowerShell Modules
  4. What is PowerShell Pipeline
  5. What are PowerShell Objects
  6. Common PowerShell Commands
  7. What are PowerShell Operators
  8. Conclusion
  9. Reference

What is PowerShell?

PowerShell is a fully functional programming and scripting language based on Microsoft .Net framework. It has built-in help and command discovery that lets you discover multiple layers of a framework. So whether you are running Windows, macOS, or Linux, you can use PowerShell for scripting.

As a scripting tool, PowerShell is commonly used for system automation for ongoing maintenance and testing. However, with the extensive functionality, it is getting popular with Azure Cloud to deploy and manage cloud resources programmatically.

What are PowerShell Commands and Cmdlets

PowerShell commands are based on lightweight functions that are designed to perform specific task sets. Therefore, cmdlets are also considered to be building blocks of PowerShell. They are designed to perform one particular action. PowerShell commands are called cmdlets, pronounced as "command-lets."

Some of the key functionalities of cmdlets are:

  • The output of cmdlets is often in the form of an object or array of objects rather than a text string.
  • The result of one command can be passed on (pipe) to another command during runtime.
  • All the commands are case-insensitive. So, for example, geT-hElp and Get-Help yields the same results.

PowerShell Command Syntax

The PowerShell commands (cmdlets) follow a convention of using a verb and a noun combined with - (dash) in between them. Therefore, the format looks like this: Verb-Noun. Thus, while working on any core commands or external library, you will see a consistent pattern on calling the commands.

For example, to query the services running on your computer, you would use Get-Service. Similarly, you can set or perform other available actions as well.

Below is a comprehensive list of cmdlet patterns for the verb section of the command:

  • Get → get the value of something
  • Set → set or update the value of something
  • New → create a new entry of something
  • Start → start something
  • Stop → stop something
  • Restart → Restart something
  • Out → output something
  • Write → write something to terminal or shell

You can always find the list of commands by typing in Get-Command and review all available commands on your computer:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/7cc08fae-b885-4ed1-8c2f-293f33c3c6ad/01-Get-Command.png
PowerShell Get-Command Output

What are PowerShell Modules

PowerShell module is a package that withholds PowerShell members, including functions, cmdlets, provider, workflow, variables, and aliases. The modules are written to solve a single set of problems or target a specific management area on a system.

Running the Get-Module -All gives you a list of all the installed modules on your computer. Refer to the example where you can see the name of the modules and all the commands each module offers:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4327c631-b7a9-40af-8599-fd7edddf8923/04-get-module.png
PowerShell Get-Module Output

Commands that use PowerShell providers don't load automatically. Instead, you need to follow a specific process to install and import the modules before they are ready to use. More on this will be covered in a later article.

What is PowerShell Pipeline

PowerShell pipeline is series of commands connected by using a | (pipe) operator. The output object of one command can serve as an input object for another command. Thus, you can chain multiple commands as long as the data is valid in the following manner:

Command-1 | Command-2 | Command-3

To visualize it with an example, run the following command inside your terminal:

Get-Process | Sort-Object -Property ProcessName -Descending
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/35410295-9d8c-4afa-be55-203245b78b59/05-getprocess-pipe.png
PowerShell Get-Process Output

What are PowerShell Objects

With the origin of PowerShell from .Net and being an object-oriented language, every call to PowerShell commands is initializing a class for that module and returning the results. Therefore, the PowerShell objects are instances of classes that have properties and methods you can call.

Here's an analogy on how to understand more:

  • Objects → set of items
  • Cmdlets → actions you can perform on those items
  • Modules → set of actions you can perform on any item
  • Properties → detail about the items
  • Methods → instructions for set of items

Common PowerShell Commands

While working with any scripting language, it's helpful to remember and familiarize yourself with some of the most common and well-known commands. In addition, most of the modules and commands come with specific instructions on how to use the module. Therefore, understanding where to find the information or help resources for any command is key to performing the task.

PowerShell Get-Verb

PowerShell Verb is an interesting concept. The idea of the verb is to define what you are trying to do with the data. Whether it's read, write, update, or insert, you are expected to follow a certain naming convention.

Run the Get-Verb command in your terminal to see the list of all the available actions:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2dcaf1ef-76ef-41f8-8603-6c826471fd0b/07-Get-Verb.png
PowerShell Get-Verb Output

The results may vary in your case. However, notice from the above image, the verbs are grouped (Group) based on their purpose and display an alias (AliasPrefix) that you can use to call the command without typing in the full name.

PowerShell Get-Member

PowerShell Get-Member command helps you display the list of methods, properties, and objects available for any command. In addition, any command that produces an output of type object can be further called upon with Get-Member to list the information.

Below is a sample command piped with Get-Member command:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c357c37a-15ae-43a7-b4a8-ca1a6eaa7261/06-getmembersample.png
PowerShell Get-Process Pipe Get-Member Output

PowerShell Get-Help

Another useful PowerShell command to be aware of in the beginning is the Get-Help command that can assist you in finding syntax and provide guidance on using all the available commands. For example, the output of Get-Help looks like this:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b3927efe-56c4-43ff-9e28-54ab0dadfd28/02-Get-Help.png
PowerShell Get-Help Output

To find the help on any command, type Get-Help, followed by any command you need help with. In the example above, the call for getting help on Get-Command looks like this:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/9446666e-e389-4b3e-8048-08a7fae52ec7/03-help-get-command.png
PowerShell Help for Get-Command Output

Looking at the output, any command that has more than one parameter set contains a SYNTAX block. The elements in the [] square brackets are optional when running the command.

What are PowerShell Operators

PowerShell operators are commands available out-of-box to perform various operations to analyze and manipulate the data. Some of the common operators include:

  • Equality → equal (-eq), not-equal (-ne), greater than (-gt), greater than or equal (-ge), less than (-lt), less than or equal (-le)
PS> 2 -eq 2                 # Output: True
PS> 2 -eq 3                 # Output: False

PS> "abc" -eq "abc"         # Output: True
PS> "abc" -eq "def"         # Output: False

PS> "abc" -ne "def"         # Output: True
PS> "abc" -ne "abc"         # Output: False
  • Matching → -like, -notlike, -match, -notmatch
PS> "PowerShell" -like    "*shell"           # Output: True
PS> "PowerShell" -notlike "*shell"           # Output: False

PS> "PowerShell" -like    "Power?hell"       # Output: True
PS> "PowerShell" -notlike "Power?hell"       # Output: False

# Partial match test, showing how differently -match and -like behave
PS> "PowerShell" -match 'shell'        # Output: True
PS> "PowerShell" -like  'shell'        # Output: False

#Regex syntax test
PS> "PowerShell" -match    '^Power\\w+' # Output: True
PS> 'bag'        -notmatch 'b[iou]g'   # Output: True
  • Containment → -in, -notin, -contains, -notcontains
"abc", "def" -contains "def"                  # Output: True
"abc", "def" -notcontains "def"               # Output: False

"Windows", "PowerShell" -contains "Shell"     # Output: False
"Windows", "PowerShell" -notcontains "Shell"  # Output: True

"abc", "def", "ghi" -contains "abc", "def"    # Output: False
"abc", "def", "ghi" -notcontains "abc", "def" # Output: True
  • Type → -is, -notis
$a = 1
$b = "1"

$a -is [int]           # Output: True
$a -is $b.GetType()    # Output: False

$b -isnot [int]        # Output: True
$a -isnot $b.GetType() # Output: True
  • Replace → -replace

Conclusion

I hope you find this useful!

Now you know what PowerShell is and understand the basics of how it works. This will go a long way if you plan to use PowerShell for either general system automation or even automating the cloud resources using Azure PowerShell.

In the mean time, check out the other articles in the PowerShell series:

Reference