PowerShell Tutorial

PowerShell Tutorial

Last updated on 19th Sep 2020, Blog, Tutorials

About author

Arunkumar (Associate Manager - Powershell )

He is A TOP Rated Domain Expert with 11+ Years Of Experience Also, He is a Technology Writer for Past 5 Years to Imparts Informative Blog for Fresher's & JOB Seeker

(5.0) | 12236 Ratings 393

PowerShell Tutorial provides basic and advanced concepts of PowerShell. Our PowerShell Tutorial is designed for beginners and professionals both.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 object 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 which combines the old command prompt (CMD) functionality with a new scripting instruction set with built-in system administration functionality.

It is designed especially for the system administrators. Its analogue 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 return .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 system 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 another 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.
Subscribe For Free Demo
[contact-form-7 404 "Not Found"]

To start the Windows PowerShell

PowerShell is available in all the latest version of Windows. We need to start PowerShell by following the given steps:

1. Search for the Windows PowerShell. Select and Click.

what-is-powershell

2. PowerShell Window will be opened.

what-is-powershell2

PowerShell Cmdlets

About Cmdlets

A cmdlet is a PowerShell command with a predefined function, similar to an operator in a programming language. Here are some key things to know about cmdlets:

  • There are system, user and custom cmdlets.
  • Cmdlets output results as an object or as an array of objects.
  • Cmdlets can get data for analysis or transfer data to another cmdlet using pipes (I’ll discuss pipes more in a moment).
  • Cmdlets are case-insensitive. For example, it doesn’t matter whether you type “Get-ADUser”, “get-aduser” or “gEt-AdUsEr”.
  • If you want to use several cmdlets in one string, you must separate them with a semicolon (;).

PowerShell Core

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

PowerShell Core is an open-source and available on the 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

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

Install PowerShell Core on Windows

The following steps are used to install the PowerShell core in Windows operating system using MSI:

1) First download the MSI file from the GitHub page of the PowerShell. Click on the following link for downloading:

2) Now, go to the Get PowerShell, and click on the .msi link according to the version of your system from the download(stable) column as shown in the below screenshot:

powershell-core

Create and Run a PowerShell ScriptCreate a PowerShell scriptOn Windows 10, a user can create the PowerShell script using any text editor or the Integrated Scripting Environment (ISE), which is already available on every system.Create a Script using NotepadThe following steps are used to create a PowerShell script:1) Open Start.2) Search for a text editor such as Notepad, and click on the Notepad app.3) Now, a new window of notepad will open. Type the script on the text file which you want to execute. For example:Echo “Hello world…..”   

create-and-run-a-powershell-script

4) Click on the ‘File’ menu in the menu bar.5) Select the option ‘save as’ from the drop-down list.6) Type the name of a file in the box and click on the ‘save’ button. For example: fs.ps17) At last, click on the save button.Create a Script using the Integrated Scripting EnvironmentThe following steps are used to create a PowerShell script using the ISE tool:1) Open Start.2) Search for the Windows PowerShell ISE, right-click on the ‘Windows PowerShell ISE’ and select the ‘Run as Administrator’ option from the drop-down list.3) Click on the File in the menu bar.4) Select the ‘New’ option to create a new empty file.5) Write a script which you want to execute. For example:Echo “Hello world…..”   

create-and-run-a-powershell-script

6) Click on the ‘File’ menu in the menu bar.7) Select the option ‘save as’ from the drop-down list.8) Type the name of a script. And, then click on the Save button.Run a PowerShell scriptTo execute the PowerShell script from the command line, follow the given steps:1) Type the Windows PowerShell in the start menu and then open it by clicking on a result.2) Now, execute the script by typing the full path to the script such as (C:/desktop/fs.ps1), or if it is in the current directory, type the file name followed by a backslash. Enable PowerShell ScriptsWhen we start the PowerShell in a computer system, the default execution policy does not allow us to execute or run the scripts.There are four different types of execution policy in PowerShell:Restricted: In this policy, no script is executed.RemoteSigned: In this policy, only those scripts are run, which are downloaded from the Internet, and these must be signed by the trusted publisher.Unrestricted: All the scripts of Windows PowerShell are run.AllSigned: Only those scripts can be run, which are signed by a trusted publisher.Because the default execution policy of Windows PowerShell is restricted, so we cannot run any script until we change it. First, we set the execution policy to Unrestricted to execute the scripts by using the following command. 

Course Curriculum

Learn Best Powershell Training to Get Most In-Demand IT Skills

  • Instructor-led Sessions
  • Real-life Case Studies
  • Assignments
Explore Curriculum

Cmdlet Format

A cmdlet always consists of a verb (or a word that functions as a verb) and a noun, separated with a hyphen (the “verb-noun” rule). For example, some of the verbs include:

  • 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 (“new” is not a verb, of course, but it functions as one)

For practice, try executing the following cmdlets:

  • Get-Process — Shows the processes currently running on your computer:
get-process
  • Get-Service — Shows the list of services with their status
  • Get-Content — Shows the content of the file you specify (for example, Get-Content C:\Windows\System32\drivers\etc\hosts)
PowerShell ArrayLike other scripting languages, Windows PowerShell also supports a data structure named as an array. An array in a PowerShell is a data structure that is used to store the collection of values in a single variable. The values in an array can be the same type or the different types. A value can be a string, an integer, a generic object, or another array.Each element or a value in an array has an index. Indexes are the integers which represent the place of a value in an array. We can retrieve the elements of an array using the index. The index of an array always starts with 0, which indicates the first item of an array.

PowerShell Variable>

Variables are the fundamental part of the Windows PowerShell. We can store all the types of values in the PowerShell variables. For example, we can store the result of commands, and the elements which are used in expressions and commands, such as paths, names, settings, and values. In fact, they store the objects specifically, Microsoft .NET Framework objects.

A variable is a unit of memory in which the data is stored. In Windows PowerShell, the name of a variable starts with the dollar ($) sign, such as $process, $a. The name of the variables are not case-sensitive, and they include spaces and special characters. By default, the value of all the variables in a PowerShell is $null.

Aliases

You can also use aliases, which are shortened cmdlet names. For instance, instead of “Get-Help” you can use just “Help”. Try running the following two commands and see whether you get the same result:

  • Start-Process notepad
  • start notepad

Similarly, to stop this process, you can use either of the following commands:

  • Stop-Process -Name notepad
  • spps -Name notepad

To see all aliases, execute the Get-Alias cmdlet.

Windows PowerShell Remoting

Using the WS-Management protocol, Windows PowerShell remoting lets you run any Windows PowerShell command on one or more remote computers. You can establish persistent connections, start interactive sessions, and run scripts on remote computers.

To use Windows PowerShell remoting, the remote computer must be configured for remote management. For more information, including instructions, see About Remote Requirements.

Once you have configured Windows PowerShell remoting, many remoting strategies are available to you. This article lists just a few of them. 

What is the PowerShell ISE?

New PowerShell cmdlet functions can be written in any text editor or word processing tool. However, the latest versions of the Windows operating system include a tool called the PowerShell ISE (Integrated Scripting Environment) to make scripting even easier and more robust.

When you open the PowerShell ISE for the first time, it may look like a familiar command prompt window. However, the tool contains much more functionality and support for writing code. The PowerShell ISE contains a full list of all the common modules and cmdlets that system administrators may need to use. When you are ready to start writing your own cmdlet functions, the debugging tool within the PowerShell ISE will allow you to test your code, identify bugs or issues, and then work to fix them. Like other coding environments, the PowerShell ISE is highly customizable. Users can choose the color scheme, font, and theme they want to use while writing scripts. New scripts created in the ISE will be given the .psi file extension which can only be run in PowerShell environments.

Powershell Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

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 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 of administrator in configuring the machines.

Disadvantages of PowerShell

Following are the few disadvantages of PowerShell:

  • Framework: It requires .NET framework.
  • Object-based: With most shells, the 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 shells, 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 disadvantages 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.

Are you looking training with Right Jobs?

Contact Us

Popular Courses