From automating repetitive tasks to carrying out advanced functions in Microsoft that aren't available, PowerShell has nearly limitless potential. Having a strong knowledge of how PowerShell works opens diverse opportunities for how it can be employed. In this PowerShell tutorial, we will unfold what PowerShell is, PowerShell commands, and many other important concepts that you need to know in order to use it.

What is PowerShell

PowerShell is Microsoft's task automation and configuration management framework. It consists of a command-line shell and is associated with a scripting language. PowerShell is based on the .NET framework. It is now an open-source project and installed on macOS, Windows, and Linux platforms.

Windows PowerShell is specifically designed for system administrators to control and automate the administration of the Windows OS and applications that run on Windows Server Environment. Cmdlets are Windows PowerShell commands that help you to manage the computers from the command line. Windows PowerShell Providers help you to access data stores such as certificate store and registry as easily as you access the file system.

Initially, using the code name "Monad," PowerShell was publicly shown at the Professional Developers Conference in September 2003. PowerShell’s first version, 1.0, was released in 2006. All major releases are still supported, as the year and version went by. PowerShell's hosting environments and capabilities are growing significantly.

PowerShell History

Initially, using the code name "Monad," PowerShell was publicly shown at the Professional Developers Conference in September 2003. PowerShell’s first version, 1.0, was released in 2006. All major releases are still supported, as the year and version went by. PowerShell's hosting environments and capabilities are growing significantly.

PowerShell Versions

Below is a list of PowerShell versions, their date of release, their features and the operating systems they support. 

  • PowerShell 1.0 - This version was released in November 2006 for Windows Server 2003 SP1, Windows Vista, and Windows XP SP2.
  • PowerShell 2.0 - This version is integrated with Windows 7 and Windows Server 2008 R2. It supports features like PowerShell remoting, background jobs, transactions, steppable pipelines, modules, script debugging, and more.
  • PowerShell 3.0 - This version is integrated with Windows 8 and Windows Server 2012. It was released as an internal part of the Windows Management Framework. It supports features like session connectivity, scheduled jobs, automatic module detection, and more.
  • PowerShell 4.0 - This version is integrated with Windows 8.1 and with Windows Server 2012 R2. New features in PowerShell 4.0 include desired state configuration, network diagnostics, enhanced debugging, and more.
  • PowerShell 5.0 - This version was released as an internal part of Windows management framework 5. The features supported in this version are PowerShell class definitions, Debugging for PowerShell Background Jobs, DSC partial configurations, and more.
  • PowerShell 6.0 - The major change in this version is its expansion to the other platforms. 
  • PowerShell 7.0 - This version is the replacement product for PowerShell Core 6.x products and is the latest version supported by the Windows PowerShell.

Uses of PowerShell

  • Windows PowerShell automates time-consuming tasks.
  • It provides network-wide workarounds
  • Allows complete access to all of the types in the .NET framework.
  • It offers a well-integrated command-line experience for the operating system.

How to launch PowerShell?

Windows PowerShell is pre-installed in all the latest versions of Windows.

1. Search for PowerShell in the Windows and select and click.

2. The following screen will open and it means PowerShell is ready to work on.

Windows PowerShell Features:

The key features of Windows PowerShell are:

  • PowerShell Remoting: The remoting feature allows scripts and cmdlets to be invoked on a remote machine.
  • Background Job: It helps to run cmdlets and scripts asynchronously on remote and local machines without affecting the interface or interacting with the console in the background.
  • Scheduled Job: It is similar to a background job, but the difference is, it must be started manually.
  • Steppable Pipeline: It allows division of script blocks into a separate steppable pipeline.
  • Script Debugging: You can set breakpoints on columns, lines, variables, functions, and commands. You can also use fic actions to run when the breakpoint is hit.
  • Evening: This Command allows you to listen, forward, and act on management and system events.
  • Error-Handling: It supports error-handling mechanism through the Catch{ }, Try{ }, and Finally { } statements.
  • Constrained Run Spaces: It supports PowerShell run spaces creation using a set of constraints, including the ability to restrict access and execution of scripts, cmdlets, and language elements.
  • Tab-Expansion: It allows autocompletion of cmdlets, parameters, and properties once the tab has been pressed.
  • Windows PowerShell Web Access: It allows PowerShell cmdlets execution from any web browser.
  • Desired State Configuration (DSC): It enables deployment and management of configuration for software and services to establish the consistency of configuration data across all devices and computers.
  • Network File Transfer: Using Background Intelligent Transfer Service (BITS) technology, PowerShell provides native support for the asynchronous, prioritized transfer of files between machines.

What is the PowerShell ISE?

Windows PowerShell ISE (Integrated Scripting Environment) is a graphical user interface that enables you to run commands and create, test, and debug scripts. It supports syntax coloring, multiline editing,  context-sensitive help, tab completion, and many more. The tool also supports the development of scripts, which are collections of commands where you can add complex logic for their execution.

PowerShell Cmdlet

A cmdlet is also known as Command let, which is a lightweight Windows PowerShell script that executes a single function. PowerShell includes cmdlets in the command prompt and you can create cmdlets Command using PowerShell APIs. Some of the key points of cmdlet are:

  • Cmdlets output results as an array of objects or an object.
  • Cmdlets can transfer data or analyze data using pipes.
  • Cmdlets are case-insensitive.
  • Use a semicolon to use several cmdlets in one string.

Cmdlet format:

A cmdlet is denoted as a verb-noun pair and has a .ps1 extension. Every cmdlet has a help file and can be obtained by typing Get-Help -Detailed.

Some of the popular cmdlets are:

  • Get — To get something
  • Set — To define something
  • Start — To run something
  • Stop — To stop something that is running
  • Out — To output something
  • New — To create something

Cmdlet vs. Command

Cmdlets are bit different from commands in other command-shell environments in the following ways:

  • Cmdlets are .NET Framework class objects and not just stand-alone executables.
  • Cmdlets are easy to construct as they contain a few lines of code.
  • Cmdlets don't handle Parsing, error presentation, and output formatting. The Windows PowerShell runtime does it.
  • Cmdlets process works on objects, not on text stream, and objects sent as output for pipelining.
  • Cmdlets are record-based as they process a single object at a time.

PowerShell Commands

Subscribe to our youtube channel to get new updates..!

There are a lot of PowerShell commands and now, we’ll discuss some of the major PowerShell commands: 

  • Get-Help 

This is the major PowerShell command every administrator should know. You can use it to get help regarding any PowerShell commands and topics.

Example: To find out all the commands you can use with the Get verb, type:

Get-Help -Name Get-*
  • Get-Command 

Get information about anything that can be invoked

Example: To generate a list of cmdlets, functions installed in your machine

Get-Command
  • Get-Service 

Finds all cmdlets with the word 'service' in it.

Example: Get all services that begin with "vm"

Get-Service "vm*"
  • Export-CSV

Export data from PowerShell into a CSV file using Microsoft Excel. The syntax is similar to that of converting a command's output to HTML.

Get-Service | Export-CSV c:service.csv
  • Stop-Process

Use this command to terminate the process. You can terminate a process based on its process ID or its name.

Stop-Process -Name notepad<br> Stop-Process -ID 2668
  • Select-Object

Use this command allows you to specify particular properties for inclusion. For example, to create a CSV file containing the name of each system service and its status, you could use the following command:

Get-Service | Select-Object Name, Status | Export-CSV c:service.csv
  • Get-EventLog

Use this command to parse your computer’s event logs.

For example, to see the application log

Get-EventLog -Log "Application"

PowerShell vs. CMD

CMD is also known as Windows Command Prompt. It is the original shell for the MS-DOS operating system and is a very old tool that does not support remote system administration.

While PowerShell presents many cmdlets for system administration tasks, it supports automation of a wide range of functions such as user and permission management, active directory administration, extracts data about security configuration, and more.

Let’s look at the major differences between PowerShell and Command Prompt:

PowerShell Command Prompt
PowerShell integrates with Windows Operating System and offers both  scripting language and command-line interface Command Prompt is a default command line interface developed by Microsoft.
PowerShell uses cmdlets that can be used either in the automation scripts or runtime environments. Command Prompt does not support any such features.
Output can be passed to other cmdlets as input through the pipeline. The output produced from a cmdlet is not a stream of text but also a collection of objects.
It is very advanced regarding capabilities, functioning, and features. It is a basic one.

PowerShell Scripts

PowerShell scripts are stored in .ps1 file. By default, you can't run a PowerShell script by merely double-clicking a file. Instead, execute a script like this: Right-click it and click "Run with PowerShell."

Also, there is a policy that limits script execution. You can check this policy by running the Get-ExecutionPolicy command.

You will see one of the following outputs:

  1. Restricted— No scripts are allowed. It is the default setting and displays the first time when you run the command.

  2. AllSigned— You can run scripts employed by a trusted developer. 

  3. RemoteSigned— You can run your scripts or scripts signed by a trusted developer.

  4. Unrestricted— You can run any script you want to run.

To change the execution policy, you need to change policy setting from restricted to remote signed using the Set-Execution RemoteSigned Policy.

  • Open an elevated PowerShell prompt. Right-click on PowerShell and "Run as Administrator"
  • Enter the following commands:

    • Get-ExecutionPolicy
    • Set-execution policy unrestricted
    • Enter Y in the prompt
    • Get-ExecutionPolicy

PowerShell Loops

PowerShell loops merely repeat the same set of commands a set number of times. Mostly, there are a number of loops available in PowerShell and more than one loop technique can be used effectively in many cases.

The major loops of PowerShell programming language are listed below:

Loop

Description

For loop

Executes a sequence of statements multiple times and compresses the code that manages the loop variable.

ForEach-loop

Enhanced for loop. This is mainly used to traverse the collection of elements including arrays.

Do..while loop

Like a while statement, but it tests the condition at the end of the loop body.

While loop

Repeats a statement or a group of statements while a given condition is true. It tests the condition before executing the loop body.

PowerShell Variables

PowerShell variables store information about all types of values stored in PowerShell. These are represented by text strings that start with a dollar sign, such as $b, $process, etc.

There are different types of variables available in PowerShell:

  • User-created variables: These are created and managed by users. By default, you can create them only in the PowerShell command line when PowerShell Window is open.
  • Automatic variables: These are created and maintained by PowerShell. Automatic variables store the state of PowerShell.
  • Preference variables: PowerShell creates these and assigns them with default values. Preference variables store user preferences for PowerShell.

The below mentioned are a few of the major variables along with their description:

Operator  Description 
$ARGS It declares undeclared parameters or parameter values passed through a function, script block or script of an array. 
$$ It denotes the last token in the last line received by the session.
$ERROR It denotes an array of error objects that represent the most recent errors.
$HOME  It denotes the full path of the user's home directory.
$FALSE It denotes false.
$NULL It represents null or empty value.
$TRUE It denotes true.
$HOST It represents the current host application of PowerShell.

PowerShell Operators

PowerShell provides various types of operators to manipulate variables. The following are the list of the PowerShell operators:

  • Arithmetic Operators

Arithmetic operators (+, -, *, /, %) are used to calculate values in a command or expression. By using this operator, you can add, multiply, subtract, or divide values, and calculate the remainder of a division operation.

  • Assignment Operators

Assignment operators (%=, +=, -=, =, *=, /=) are used to change, assign, or append values to variables. 

  • Comparison Operators

Comparison operators ( -le, -ne, -gt, -lt, -eq, -ge) are used for comparing values and test conditions. The comparison operators also include operators that find or replace patterns in text. 

  • Logical Operators

Logical operators ( -or, -xor, -and, -not, !) are used to connect conditional statements into a single complex conditional. 

  • Redirection Operators

These operators (>>, >, 2>>, 2>, and 2>&1) are used to send the output of a command or expression to a text file. It works like the Out-File cmdlet (without parameters), but also will let you redirect error output to specific folders. 

  • Split and Join Operators

These operators are used to divide and combine substrings. The -join operator concatenates multiple strings into a single string. The -split operator splits a string into substrings. 

  • Type Operators

Type operators ( -isnot, -is, -as) are used to find or change the .NET Framework type of an object.

  • Unary Operators

Unary operators are used to increment or decrement variables or object properties and sets integers as positive or negative numbers. 

Conclusion:

In this blog, we have covered all the primary concepts of PowerShell and how it has become the most important programming language. We hope this tutorial is informative and adds value to your knowledge.