How To Learn Microsoft PowerShell Scripting Tutorial for beginners

By: Microtek Learning

281

How To Learn Microsoft PowerShell Scripting Tutorial for beginners

Table of Content

What is PowerShell?

Windows PowerShell is a scripting language and object-oriented automation engine. It is mainly intended for system administrators and professionals to manage and automate the management of the Windows operating system and other application. It introduces appealing new concepts built on the knowledge and scripts you've developed in the Windows Script Host environments and Windows Command Prompt.

It combines the capability of a GUI-based admin tool, command-line speed, and flexibility of scripting. It enables you to fix problems quickly by supporting system admin in reducing future manual labour hours. We'll go through all the key points you need to know in order to understand PowerShell.

Why Should You Use PowerShell?

Here are a few compelling reasons to use PowerShell:

  • PowerShell offers an integrated command-line experience for the operating system.
  • PowerShell provides full access to the.NET framework's types.
  • System administrators rely on it.
  • PowerShell is a command-line interface for managing server and workstation components.
  • It's aimed toward system administrators with more straightforward syntax.
  • Running PowerShell instead of VBScript and other scripting languages is more secure.

History of PowerShell

Version 1.0 of PowerShell was released in 2006, and Version 7.2 of PowerShell is the most recent version. PowerShell's features and hosting environments expanded significantly as the version and year went by.

Let's look at PowerShell's version history:

  • Version 1 of PowerShell allowed local administration of Windows-Server 2003.
  • Version 2.0 of PowerShell was combined with Windows Server 2008 R2 and Windows 7. This version introduces remoting and expands PowerShell's capabilities, including transactions, background jobs, debugging, and events.
  • Version 3.0 of PowerShell was made available as part of the Windows administration framework and was included on Windows Server 2012 and Windows 8. You can, among other things, schedule and create jobs, connect to a session, and load modules automatically.
  • Version 4.0 of PowerShell was integrated with Windows Server 2012 R2 and Windows 8.1. This version counted support for enhanced debugging, desired state configuration, and network diagnostics.
  • Version 5.0 of PowerShell was released as an internal component of Windows-management-framework 5. This version has many features such as .NET enumerations, class definitions, remote debugging, etc.
  • PowerShell 7.2 is now available. It's made with.NET 6.0. This version provides a dynamic and simplified error view, new operators, new version notifications, etc.

PowerShell Features

PowerShell Remoting: PowerShell allows you to run scripts and cmdlets on a remote system.

Background Jobs: It allows you to asynchronously run a script or pipeline. You can operate your jobs on a single machine or numerous devices controlled remotely.

Transactions: Allow developers to use the cmdlet by enabling it.

Evening: You can use this command to listen to management and system events, forward them, and take action.

Network File Transfer: PowerShell uses the Background Intelligent Transfer Service (BITS) technology to provide throttled, asynchronous native support for prioritizing and moving files between machines.

How Do I Start PowerShell?

We will now learn how to start PowerShell on Windows OS.

All current versions of Windows come with pre-installed PowerShell. To start PowerShell, follow these steps:

Step 1: In Windows, look up PowerShell. and click

Step 2: Opens the PowerShell Window

PowerShell Cmdlet

In the Windows PowerShell environment, a cmdlet (also known as a commandlet) is a lightweight command. In the command prompt, PowerShell calls these cmdlets. Using PowerShell APIs, you may construct and run cmdlets commands.

Command VS Cmdlet

In many other command-shell environment, Cmdlets differ from commands in the following ways.

  • Cmdlets are class objects in the.NET Framework. It cannot be done individually.
  • Cmdlets can be built with only a few lines of code.
  • Cmdlets do not handle parsing, error presentation, or output formatting.
  • The Cmdlets process is applied to objects. As a result, text streams and objects cannot be used as pipelining output.
  • Cmdlets are record-based, which means only one thing is processed at a time.

Cmdlets, which are always in verb-noun structure and not plural, provide most PowerShell functionality. Cmdlets also return objects rather than words. A cmdlet is a set of commands that span many lines and are stored in a text file with the.ps1 extension.

A hyphen should always be used to split a verb and a noun in a cmdlet. You can learn PowerShell by using the following verbs:

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

PowerShell Commands

Here is a list of helpful PowerShell commands:

Get-Help: PowerShell command and topic help

Example: Show help details regarding the command Format-Table.

Get-Help Format-Table

Get-Command: Get details about anything that can be triggered.

Example: To get a list of cmdlets and functions installed on your machine, type cmdlets into the search box.

Get-Command

Get Service: Finds all cmdlets that contain the word 'service.'

Example: Get a list of all services that start with "vm"

Get-Service "vm*"

Get Member: Demonstrate what an object can do.

Example: Obtain the VM processes' members.

Get-Service "vm*" | Get-Member

Other Commands: 

  • Displays command packages.
  • Get Content This cmdlet can process a file's contents and then perform anything with them.
  • Obtain All cmdlets that begin with 'get-' 

Example: Make a folder.

New-Item -Path 'X:\Microtek99' -ItemType Directory

Output:

PowerShell Data types

 

Special Variables

PowerShell Scripts

.ps1 files are used to store PowerShell scripts. You can't launch a script by double-clicking it by default. This safeguards your system from danger. To run a script:

Step 1: Right-click it and select "Run with PowerShell" from the menu.

Furthermore, script execution is restricted by policy. The Get-Execution Policy command will show you this policy.

You'll get the below output:

  • Restricted: It is not permitted to use scripts. Because this is the defaulting configuration, it will appear when you execute the command for the first time.
  • All Signed: Scripts signed by a reputable developer can be run. This setting causes a script to ask for confirmation before running.
  • Remote Signed: You can execute your own or trusted developer's scripts.
  • Unrestricted: You are free to run whatever script you choose.

How to Change Your Execution Policy

Step 1: Open a PowerShell prompt, then on PowerShell, right-click and select Run as Administrator.

Step 2: Enter the commands shown below:

  1. Get-Execution Policy
  2. Enter Y in the prompt
  3. Set-execution policy unrestricted
  4. Get-Execution Policy

First PowerShell Script

Put the following command into a notepad.

Write-Host "Hello, Microtek Learning!”

ps1 is the extension for PowerShell scripts. Name the file FirstScript.ps1 and save it.

What is PowerShell ISE?

The defaulting editor for Windows-PowerShell is Windows PowerShell Integrated-Scripting-Environment (ISE). You can use this ISE to execute commands, debug scripting and test them in a window-based GUI. You can do syntax colouring, multiline editing, selective execution, tab completion, and other available features.

You can also run commands on the console pane with Windows PowerShell ISE. It does, however, support windows that you can utilize to inspect the source-code of your scripts and other tools that you may pack into the Integrated Scripting Environment at the same time. variable You can even run numerous script windows at once. This is quite useful when debugging a script that calls functions from other modules or scripts.

The identical script can be written in ISE as it was in Notepad.

1 In the editor, paste the code.

2 Save the Script

3 To run the script, press F5.

4 Analyse the console output.

Sample 2:

The below code will provide you with a Free Virtual Memory on your machine.

Get-WmiObject -Class Win32_OperatingSystem –ComputerName localhost |
Select-Object -Property CSName,FreeVirtualMemory 

PowerShell Concepts

Benefits of Scripting in PowerShell

  • PowerShell scripts are extremely powerful and can do a lot with a few lines of code.
  • Variables are declared using the $variable> syntax.
  • Command output, objects, and values could all be stored in variables.
  • It is not instructed to state a variable's "type."

PowerShell Vs. Command Prompt

PowerShell Applications

PowerShell Certification Training is a popular choice among IT managers because it reduces the time and effort required to operate large corporate networks. For example, Assume you're in charge of an extensive network with over 400 servers. Now you wish to execute a new security solution, and this security solution relies on a specific service that must run on those servers.

You may certainly log in to each server and check whether or not that service is installed and operational. However, it takes quite a bit of human error because your employees must spend a lot of time on this inefficient process.

If you utilize PowerShell, though, you can do this task in a matter of minutes. This is because the entire operation is controlled by a single script that gathers information on the servers' services.

If you utilize PowerShell, though, you can do this task in a matter of minutes. This is because the entire operation is controlled by a single script that gathers information on the servers' services.

Summary

  • PowerShell is a scripting-language and automation engine for Windows.
  • For the operations system, PowerShell provides a well-blended command-line experience.
  • The initial version 1.0 of PowerShell was released in 2006.
  • PowerShell allows you to run scripts and cmdlets on a remote system.
  • PowerShell comes pre-installed in all recent Windows editions.
  • A cmdlet is a simple command that operates in Windows-PowerShell.
  • PowerShell commands such as Get, Stop, Out, Start, Set, and New are essential.
  • PowerShell data types include Boolean, Chat, Byte, Decimal, Decimal, and Long.
  • $Profile, $Error. $Host, $PSUICulture, $PID, and $NULL are some PowerShell variables.
  • The defaulting editor for PowerShell is Windows PowerShell ISE.
  • PowerShell is intimately integrated with the Windows operating system, whereas Command Prompt is Microsoft's default command-line interface.
  • PowerShell has been a popular choice among IT administrators because it reduces the time and effort required to administer large corporate networks.
Leave a message here

Get In Touch
Are you being sponsored by your employer to take this class?
* I authorize Microtek Learning to contact me via Phone/Email