Windows PowerShell Tutorial

 PowerShell Tutorial

PowerShell Tutorial provides basic and advanced concepts of PowerShell. Our PowerShell Tutorial is designed for both beginners and professionals.

Windows PowerShell is a command-line shell and associated scripting language created by Microsoft. It is built on the .NET framework. The commands in the Windows PowerShell are called cmdlets, which allow you to manage the computer from the command line.

Our PowerShell tutorial includes all the PowerShell topics such as PowerShell scripting, variables, array, loops, commands, including objects and classes.

What is PowerShell

Windows PowerShell is an automated task-based command-line shell and associated scripting language created by Microsoft. It is the new shell of Microsoft that combines the old command prompt (CMD) functionality with a new scripting instruction set with built-in system administration functionality.

It is designed especially for system administrators. Its analog in Linux OS is called Bash scripting. Unlike other shells, which accept and return text, it is built on the top of the .NET framework, CLR (Common Language Runtime) and DLR (Dynamic Language Runtime). So, it can accept and returns .NET Framework objects.

It helps IT, professionals, to control and automate the administrations of the Windows operating system and other applications which run on the Windows server environment. PowerShell enables administrators to perform tasks on remote and local Windows systems through full access to COM and WMI.

The commands in the Windows PowerShell are referred to as cmdlets pronounced as "command-lets", where each cmdlet represents a specific function or task-based scripts.

Like many other shells, it gives us access to the file system on the computer system. Also, providers of Windows PowerShell enable us to access other data stores, such as the registry and the digital signature certificate stores.

Why use PowerShell?

  • It is both a scripting language and a command-line Shell.
  • It can interact with a different number of technologies.
  • Windows PowerShell allows complete access to all the types in the .NET framework.
  • PowerShell is object-based.
  • Many interfaces of GUI that Microsoft designed for its various products are front-end interfaces to PowerShell.
  • It is more secure than running VBScript or other scripting languages.
  • It allows performing repetitive tasks more efficiently by combining multiple commands and by writing scripts. Suppose, a system administrator wants to create hundreds of active directory users, he can achieve this with the help of only some PowerShell cmdlets placed in a script.
  • Many complex and time-consuming configurations and tasks can be done in a second with simple cmdlets of PowerShell.

Advantages of PowerShell

Following are the few benefits of a PowerShell:

  • It is easy to learn and implement.
  • It is an object-based scripting language.
  • It provides more functions as compared to VBScript and cmd.exe
  • PowerShell supports an automation platform, which is also an important factor.
  • We can also execute a .NET code in PowerShell.
  • In PowerShell, there is no need to specify the "type" of a variable.
  • It is also interactive. It allows programmers to try first at a console and then work with more complicated scripts.
  • There is a concept of background jobs in PowerShell scripting.
  • It provides the feature of Reusability. The scripts which are written once to perform a particular task can be saved for later use. And can be merged with other scripts to perform various tasks. So, repetitive tasks can be performed to execute the tasks. It saves time and money for administrators in configuring the machines.

Disadvantages of PowerShell

Following are the few disadvantages of PowerShell:

  • Framework: It requires a .NET framework.
  • Object-based: With most shells, text-based commands are used to get the work done while writing scripts. If a user switches to Windows PowerShell from some other type of shell, he will have to get used to a different way of thinking. Due to this, some users need more time to understand the PowerShell.
  • Security risks: Another drawback of using PowerShell is that it can create some potential security risks. Many professionals of IT use it as a way to connect remotely to other servers and computers. During this process, PowerShell can leave some holes open for security breaches. It is the major disadvantage of using PowerShell script.
  • Web Server: Another drawback of PowerShell is that it requires a user to run a web server on his server when utilizing remote functionality.

Prerequisite

Before learning PowerShell, you must have a basic knowledge of computers and the Windows operating system.

Features of PowerShell

Before beginning with the deeper concept of PowerShell, let's take a look at the significant features of PowerShell scripting language that make it different from other scripting languages.


  1. Windows PowerShell Workflow: Workflow capabilities have been introduced in Windows PowerShell version 3.0. It is designed particularly to help us by performing long-time and effort-consuming complex tasks across different and multiple devices at different locations.
  2. Desired State Configuration: DSC is the feature of a Windows PowerShell which provides a collection of language extensions, resources, and cmdlets that can be used to configure the software declaratively.
  3. Background job: Windows PowerShell introduced the concept of background jobs that run scripts and cmdlets asynchronously on the remote and local machines in the background without affecting the user interface or interacting with the console.
  4. Scheduled job: It is similar to the background job. Both the jobs execute asynchronously in the background without affecting the interface, but the main difference is that the background jobs are started manually.
  5. Error-handling: Windows PowerShell provides the error-handling mechanism through the Try{ }, Catch { }, and Finally {} blocks as in .NET language.
  6. PowerShell remoting: This feature of Windows PowerShell allows the execution of cmdlets on remote systems which helps to manage the set of remote computers from one single machine.
  7. Script debugging: It is a feature of PowerShell to examine the script, function, command, or expressions while PowerShell is running. PowerShell script debugger includes the set of cmdlets that allow to set and manage breakpoints and view the call stack.
  8. Tab expansion: Tab expansion is an implementation of auto-completion, which completes the cmdlets, properties, and parameter names by pressing the Tab key once.
  9. Steppable pipeline: This feature allows the splitting of script blocks into a steppable pipeline. And then it gives the option to call the begin(), process(), and end() methods of script block to control the execution sequence.
  10. Constrained run spaces: It allows the creation of PowerShell run spaces with a set of constraints that includes the ability to access and execution of scripts, cmdlets, and language elements.
  11. Windows PowerShell web access: PowerShell console introduced a web-based version in Windows Server 2012. Here, we can run PowerShell cmdlets from any web browser which is not available on desktops but also on any tablet or mobile device.
  12. Network file transfer: This feature provides the native support for prioritized, and asynchronous transfer of files between the machines using BITS (Background Intelligent Transfer Service).
  13. Windows PowerShell Integrated Scripting Environment (ISE): It is the host GUI-based application for Windows PowerShell. This feature provides tab completion, multiline editing, syntax coloring, context-sensitive help, selective execution, and support for right-to-left languages.
  14. Transactions: This feature of Windows PowerShell enables us to start a transaction, indicate which command is a part of it, and either rollback or commit a transaction.

History of Windows PowerShell

By the year 2002, Microsoft started developing a new way of managing the command lines. It also included the development of a new Shell which was named Monad. A white paper published in the year 2002, called Monad Manifesto. It contained the concept of this shell and the ideas to create a standardized platform that used the .NET framework through automation tasks.

The first display of Monad was made in Los Angeles at the Professional Developers Conference in 2003. Microsoft released the three beta versions of Monad on 17th of June, 2005, 11th of September, 2005, and on 10th of January 2006. Microsoft renamed Monad to a Windows PowerShell in April during the same year and made it a core part of the Windows operating system.

Versions of Windows PowerShell

PowerShell 1.0:

In November 2006, PowerShell version 1.0 was released by Microsoft for Windows Vista, Windows XP SP2, and Windows Server 2003 SP1. This version is an optional component of Windows Server 2008.

PowerShell 2.0:

PowerShell version 2.0 is integrated with Windows Server 2008 R2 and Windows 7. It is released for Windows Vista with Service Pack 1, Windows Server 2003 with Service Pack 2, and Windows XP with Service Pack 3.

This version includes changes to the hosting API and scripting languages.

Following are the new features of PowerShell 2.0:

  • PowerShell remoting
  • Background jobs
  • Steppable pipeline.
  • Script Debugging
  • Windows PowerShell ISE (Integrated Scripting Environment)
  • Network file transfer

PowerShell 3.0:

PowerShell version 3.0 is integrated with Windows Server 2012 and Windows 8.

Microsoft has also made it available for Windows Server 2008 with Service Pack 1, for Windows 7 with Service Pack 1, and for Windows Server 2008 R2 with Service Pack1.

This version is an internal part of the (WMF3) Windows Management Framework package, which contains the WinRM services to support remoting.

Following are the new features of PowerShell 3.0:

  • Scheduled Jobs
  • Session connectivity
  • Delegation support
  • Automatic Module Detection
  • Improved code Writing
  • Help update
  • New commands

PowerShell 4.0:

PowerShell version 4.0 is integrated with Windows Server 2012 R2 and Windows 8.1.

Microsoft has also made it available for Windows Server 2008 R2 SP1, Windows 7 SP1, and Windows Server 2012.

Following are the new features of PowerShell 4.0:

  • Desired State Configuration (DSC)
  • A new default execution policy
  • Save-help
  • Enhanced debugging
  • Network diagnostics

PowerShell 5.0:

This version was released to the web on 24th February 2016, as an internal part of the Windows Management Framework (WMF 5.0).

This version is installed by default on Windows 10 and Windows Server 2012.

Following are the new features of PowerShell 5.0:

  • PowerShell .NET enumerations
  • DSC partial configurations
  • Debugging for PowerShell background jobs
  • Debugging for PowerShell Runspaces in remote processes
  • PowerShell class definitions (properties, methods)

PowerShell 5.1:

PowerShell 5.1 was released on the 2nd of August 2016, along with the Windows 10 Anniversary update. This version is finally released on 19 January 2017. Microsoft has made it available for Windows 7, Windows Server 2008, Windows Server 2008 R2, Windows 7, Windows Server 2012, and Windows Server 2012 R2.

This is the first version that came in two editions of "Core" and "Desktop".

#PowerShellCommand Prompt (CMD)
1It is the automated task-based command-line interface and associated scripting language based on the .NET framework.It is the default command-line interpreter for the Microsoft Windows operating system.
2It can interpret both batch and PowerShell commands.It can interpret only batch commands.
3It is used to control and automate the applications and Windows operating system on a Windows server.It is used to execute the given commands on the console, which can be used to debug the problem.
4The output generated by the PowerShell is not just a stream of characters but a collection of objects.The output generated by the command prompt is just a stream of characters(text).
5It is both a shell and scripting environment that supports the creation of large files for managing the windows operating system.It is just a shell system, which allows a user to do only easy and basic scripts for the execution of the batch file.

PowerShell vs. Bash Shell comparison table

#PowerShellBash Shell
1PowerShell is a command shell and associated scripting language for the majority of Windows operating systems.Bash is the command shell and scripting language for the majority of the Linux operating system.
2PowerShell was introduced in 2006 with its first version.Bash shell was introduced in 1989.
3It treats input and output as an object.It always accepts input and output as a text structure.
4The user interface of PowerShell is a graphical command-line interface CLI.The user interface of Bash shell is a text-based command-line interface.
5It can execute on any version of Windows from Windows 97 to Windows 10.It is mainly prepared for Linux and Unix operating system from the first day

Comparison of Bash vs. PowerShell Commands

#PowerShellCommand Prompt (CMD)
List files and folderslsLs, Get-childItem
Change directorycdSet-childItem
Show Working directorypwdGet-Location
Clear the ScreenClearcls, clear
To copy a filecpCopy-Item
To remove or delete a filermRemove-Item, ri, rmdir, rd, del, rm
To print a stringecho "string"Write-host "string" OR
echo "string"
To create a new text filetouch <filename.txt>New-Item <filename.txt>

Windows PowerShell ISE

The Microsoft Windows PowerShell ISE is a graphical user interface-based application and a default editor for Windows PowerShell. ISE stands for the Integrated Scripting Environment. It is an interface in which we can run commands and write, test, and debug PowerShell scripts without writing all the commands in the command-line interface.

The Integrated Scripting Environment (ISE) provides tab completion, multiline editing, syntax coloring, context-sensitive help, selective execution, and support for right-to-left languages.

Windows PowerShell ISE is a part of a WMF (Windows Management Framework), which allows administrators for managing the multiple version of Windows and Windows servers. It allows us to execute the commands in the console pane. However, it also supports those panes which are used to simultaneously view the source code of the script and other tools which can plug into the ISE.

We can even open multiple script windows at the same time. It is specifically useful when we are debugging a script that uses the function defined in other modules or scripts.

The Integrated Scripting Environment (ISE) was first introduced with Version 2.0 of Windows PowerShell and was re-designed with PowerShell version 3.0.

Key features

Following are the key features of Windows PowerShell ISE:

  • Multiline editing: In the command pane, to insert a blank line under the current line, press SHIFT + ENTER.
  • Selective Execution: To execute a part of a script, select the text to run, and click the Run script Or, press the F5 key.
  • Context-sensitive help: Type the Invoke-Item and then press the f1 A help file opens to the article for an Invoke-Item command let.
  • The ISE window of PowerShell consists of the following three panes:
  • Script Pane: This pane allows the users to create and run the scripts. A user can easily open, edit, and run the existing scripts in the script pane.
  • Output Pane: This pane displays the outputs of the scripts and the commands which are run by you. You can also clear and copy the contents in the Output pane.
  • Command Pane: This pane allows the users to write the commands. You can easily execute the single or multiple-line command in the command pane.

PowerShell Core

PowerShell version 5.1 is the latest release of the original PowerShell, but starting with PowerShell version 6.0, it is now referred to as a PowerShell core.

PowerShell Core is open-source and available on Windows, Linux, and Mac operating systems. It is built on the .NET core.

PowerShell core is launched as pwsh on Linux and MacOS and pwsh.exe on Windows.

PowerShell Core version 6.0 was first introduced on 18 August 2016.

Differences Between PowerShell and PowerShell Core

The given table describes the differences between the PowerShell and PowerShell core:

AttributesPowerShellPowerShell Core
Versions1.0 to 5.16
Platform SupportedWindowsLinux, MacOS, and Windows
Dependency.NET Framework.NET Core
Launched aspowershell.exepwsh for MacOS and Linux, and pwsh.exe for Windows
UsesIt relies on .NET framework runtime.It depends on the .NET core runtime.
$PSVersionTable.PSEditionIt is set to DesktopIt is set to Core.

Below are the reference video links gathered for Learning Windows Power Shell Scripting. Have a look at them by clicking "Watch Now".

S NoTopicDescriptionRef Link
1Power Shell OverViewIntroduction to Power Shell, Difference between Power shell and another scripting,
Basic Commands
Watch Now
2DifferenceDifference between cmdlets vs CommandWatch Now
3Files and FoldersCreation of files, folders
Moving files and folders
Deleting files and folders
Renaming files and folders
Checking existence of files and folders
Watch Now
4Advanced cmdletscmdlets types and descriptionWatch Now
5Power shell scriptingFeatures, Creating and using of variablesWatch Now
6operatorsTypes of operatorsWatch Now
7
Looping
Types of loops
Watch Now
Watch Now
8
Conditions
Types of conditions
Watch Now
Watch Now
Watch Now
9ArraysDeclaring and processing arraysWatch Now
10HashvariablesDeclaring and processing hashvariablesWatch Now
11Regexregular expressionsWatch Now
12Exporting to CSV and XML.movconverting command output and saving as CSV
or XML file
Watch Now
13Diff (Compare-Object)comparing two sets of objectsWatch Now
14ConvertTo-HTMLConverts .NET objects into HTML that can be displayed in a Web browser.Watch Now
15
Snap-in
Description and adding snap-in
Watch Now
Watch Now
16Product-Specific ShellsProduct-Specific ShellsWatch Now
17Modulescreating power shell scriptsWatch Now
18
Pipeline Input
Pipeline InputWatch Now
Input via ParenthesesWatch Now
Fix User InputWatch Now
ByValueWatch Now
ByPropertyNameWatch Now
19Formatting DefaultsFormatting DefaultsWatch Now
20
Remoting
Local RemotingWatch Now
Enabling RemotingWatch Now
1-to-Many RemotingWatch Now
1-to-1 RemotingWatch Now
21Exploring WMIRemote connections in WMIWatch Now
22
Jobs
Working with Child JobsWatch Now
Starting Local JobsWatch Now
Jobs Remote ComputersWatch Now
23
Reusing the output
Write-OutputWatch Now
Write-Host.Watch Now
Read-HostWatch Now
24Try...Catchuse the Try, Catch, and Finally blocks to handle terminating errors.Watch Now

Reference Links:

https://www.javatpoint.com/powershell

https://www.youtube.com/playlist?list=PL6D474E721138865A

Comments

Popular posts from this blog

Informatica MDM Tutorial