PowerShell Tutorial

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 open 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.

 Topics that we will be covering in this PowerShell tutorial:

PowerShell Tutorial for Beginners

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 is 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 the 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 stores and registries 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.

Explore- PowerShell Interview Questions

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 Windows PowerShell.

MindMajix Youtube Channel

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 Windows and select and click.

Blog post image

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

Blog post image
Explore - PowerShell Operators

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 the 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 <cmdlet-Name> -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 a 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

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-*
Explore - Basic PowerShell commands

PowerShell Vs. CMD

 

Blog post image

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:

PowerShellCommand 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 a .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."

Blog post image

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

Blog post image

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 the 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"Blog post image
  • Enter the following commands:

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

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:

 

 

LoopDescription
For loopExecutes a sequence of statements multiple times and compresses the code that manages the loop variable.
For loopEnhanced for a loop. This is mainly used to traverse the collection of elements including arrays.
ForEach-loopLike a while statement, but it tests the condition at the end of the loop body.
ForEach-loopRepeats 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.

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

Operator Description 
$ARGSIt 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.
$ERRORIt denotes an array of error objects that represent the most recent errors.
$HOMEIt denotes the full path of the user's home directory.
$FALSEIt denotes false.
$NULLIt represents a null or empty value.
$TRUEIt denotes true.
$HOSTIt 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 ( -is not, -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 set 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.

Are you looking to get trained on PowerShell, we have the right course designed according to your needs. Our expert trainers help you gain the essential knowledge required for the latest industry needs. Join our PowerShell Certification Training program in your nearest cities.

PowerShell Training Chennai, PowerShell Training Bangalore, PowerShell Online Training in Hyderabad, PowerShell Training Pune.

These courses are equipped with Live Instructor-Led Training, Industry Use cases, and hands-on live projects. This training program will make you an expert in PowerShell and help you to build your career as a PowerShell Developer. Additionally, you get access to Free Mock Interviews, Job and Certification Assistance by Certified PowerShell Trainers. 

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
PowerShell TrainingNov 19 to Dec 04View Details
PowerShell TrainingNov 23 to Dec 08View Details
PowerShell TrainingNov 26 to Dec 11View Details
PowerShell TrainingNov 30 to Dec 15View Details
Last updated: 08 Jan 2024
About Author

 

Madhuri is a Senior Content Creator at MindMajix. She has written about a range of different topics on various technologies, which include, Splunk, Tensorflow, Selenium, and CEH. She spends most of her time researching on technology, and startups. Connect with her via LinkedIn and Twitter .

read less