Powershell Tutorial for Beginners

โšก Smart Summary

PowerShell Scripting is an object-oriented automation engine built on the .NET framework. It combines cmdlets, scripts, and a scripting language to help IT administrators automate Windows, manage services, and connect remote machines from a single command line.

  • ๐Ÿš€ Launch the right shell: Open PowerShell or PowerShell ISE from the Start menu, and run it as Administrator when you need elevated privileges.
  • ๐Ÿงฉ Use cmdlets, not text commands: Cmdlets follow a verb-noun naming pattern and return .NET objects, so output can flow into the next cmdlet through a pipeline.
  • ๐Ÿ” Set execution policy carefully: Use Set-ExecutionPolicy with RemoteSigned to balance safety and convenience when running .ps1 scripts.
  • ๐Ÿ“ Write reusable .ps1 scripts: Combine cmdlets, variables, and pipelines in a text file with the .ps1 extension to automate repetitive admin tasks.
  • ๐Ÿค– Pair PowerShell with AI: AI assistants turn plain-English requests into ready-to-run PowerShell scripts and explain unfamiliar cmdlets line by line.

Powershell Tutorial for Beginners

What is PowerShell?

Windows PowerShell is an object-oriented automation engine and scripting language. It is designed for IT professionals and system administrators who need to control and automate the administration of Windows and other applications. PowerShell extends the concepts learned in Windows Command Prompt and Windows Script Host with new objects, cmdlets, and a powerful pipeline.

It combines the flexibility of scripting, the speed of a command line, and the depth of a GUI admin tool. With it, administrators can solve problems efficiently and eliminate hours of manual work. This tutorial walks through the essentials you need to learn PowerShell from scratch.

Why Use PowerShell?

The most important reasons to use PowerShell are:

  • Offers a well-integrated command-line experience for the operating system.
  • Provides complete access to every type in the .NET framework.
  • Trusted by system administrators across enterprises.
  • Lets you manipulate server and workstation components with a few lines.
  • Geared toward system administrators with a clean verb-noun syntax.
  • More secure than running VBScript or other legacy scripting languages.

PowerShell History

PowerShell 1.0 was first released in 2006. PowerShell is now at version 7.2 and beyond. As releases shipped, its capabilities and hosting environments grew significantly.

Version-wise history of PowerShell:

  • PowerShell 1.0 โ€” supported local administration of Windows Server 2003.
  • PowerShell 2.0 โ€” integrated with Windows 7 and Windows Server 2008 R2. Added remoting, transactions, background jobs, events, and debugging.
  • PowerShell 3.0 โ€” shipped with Windows 8 and Windows Server 2012 as part of the Windows Management Framework. Added scheduled jobs, session connectivity, and automatic module loading.
  • PowerShell 4.0 โ€” shipped with Windows 8.1 and Windows Server 2012 R2. Added Desired State Configuration, enhanced debugging, and network diagnostics.
  • PowerShell 5.0 โ€” part of Windows Management Framework 5. Added remote debugging, class definitions, and .NET enumerations.
  • PowerShell 7.2 โ€” built on .NET 6.0. Adds new operators, dynamic error views, and automatic version notifications.

Features of PowerShell

  • PowerShell Remoting: invoke scripts and cmdlets on a remote machine.
  • Background Jobs: run scripts or pipelines asynchronously on the local machine or many remote machines.
  • Transactions: let cmdlets and developers perform grouped operations that can be committed or rolled back together.
  • Eventing: listen for, forward, and act on management and system events.
  • Network File Transfer: native support for prioritized, asynchronous, throttled file transfer between machines through the Background Intelligent Transfer Service (BITS).

How to Launch PowerShell

PowerShell is pre-installed on every recent version of Windows. Launch it as follows.

Step 1) Search for PowerShell in Windows, then select and click it.

Launch PowerShell

Step 2) The PowerShell window opens.

PowerShell window

Cmdlet vs. Command

A cmdlet (pronounced “command-let”) is a lightweight command used in the PowerShell environment. PowerShell invokes cmdlets at the prompt, and you can create your own using the PowerShell APIs. Cmdlets differ from traditional shell commands in the following ways:

  • Cmdlets are .NET Framework class objects; they cannot be executed as standalone executables.
  • Cmdlets can be written in as few as a dozen lines of code.
  • Parsing, output formatting, and error presentation are handled by the runtime, not by the cmdlet itself.
  • Cmdlets work with objects, so text streams and objects can both flow through pipelines.
  • Cmdlets are record-based and process a single object at a time.

Most PowerShell functionality is delivered through cmdlets, which always use a verb-noun naming pattern (always singular). Cmdlets return objects, not text. A script โ€” distinct from a cmdlet โ€” is a series of commands stored in a text file with the .ps1 extension.

Common verbs you will use to learn PowerShell include:

  • Get โ€” retrieve something.
  • Start โ€” run something.
  • Out โ€” output something.
  • Stop โ€” stop something that is running.
  • Set โ€” define something.
  • New โ€” create something.

Important PowerShell commands

Get-Help: view help for PowerShell commands and topics.

Example: display help for the Format-Table command.

Get-Help Format-Table

Get-Help cmdlet

Get-Command: get information about anything that can be invoked.

Example: generate a list of cmdlets and functions installed on the machine.

Get-Command

Get-Command cmdlet

Get-Service: finds all services on the machine.

Example: get every service whose name begins with “vm”.

Get-Service "vm*"

Get-Service cmdlet

Get-Member: show what can be done with an object.

Example: list members of the vm services returned by Get-Service.

Get-Service "vm*" | Get-Member

Get-Member cmdlet

Other useful cmdlets:

  • Get-Module โ€” show installed packages of commands.
  • Get-Content โ€” read a file and process its contents.
  • Get-* wildcard โ€” list every cmdlet that starts with “Get-“.

Example: create a folder.

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

Output:

New-Item output

PowerShell Data Types

PowerShell supports the standard .NET data types so you can store any value cleanly in a variable.

PowerShell Data Types

Special Variables

PowerShell exposes a number of built-in special variables. The most useful ones are listed below.

Special Variable Description
$Error An array of error objects representing the most recent errors.
$Host Displays the name of the current hosting application.
$Profile Stores the full path of the user profile for the default shell.
$PID Stores the process identifier.
$PSUICulture Holds the name of the current UI culture.
$NULL Contains the empty or null value.
$False Contains the boolean FALSE value.
$True Contains the boolean TRUE value.

PowerShell Scripts

PowerShell scripts are stored in .ps1 files. By default, you cannot run a script by double-clicking it โ€” this safeguard protects the system from accidental harm. To execute a script, right-click it and choose “Run with PowerShell”.

Run with PowerShell

An execution policy controls which scripts are allowed to run. View the current policy with Get-ExecutionPolicy. Possible values include:

  • Restricted โ€” no scripts are allowed. This is the default.
  • AllSigned โ€” only scripts signed by a trusted developer run, and each requires confirmation.
  • RemoteSigned โ€” your own scripts run, plus signed scripts from trusted sources.
  • Unrestricted โ€” any script can run (use sparingly).

Steps to change execution policy:

Step 1) Open an elevated PowerShell prompt by right-clicking PowerShell and selecting Run as Administrator.

Run PowerShell as Administrator

Step 2) Run the commands below.

  1. Get-ExecutionPolicy
  2. Set-ExecutionPolicy Unrestricted
  3. Enter Y at the confirmation prompt.
  4. Get-ExecutionPolicy

Change execution policy

First PowerShell Script

Open Notepad and type:

Write-Host "Hello, Guru99!"

Save the file with a .ps1 extension โ€” for example, FirstScript.ps1.

Save FirstScript.ps1

In PowerShell, run the script with:

& "X:\FirstScript.ps1"

Run FirstScript.ps1

What is PowerShell ISE?

The Windows PowerShell Integrated Scripting Environment (ISE) is the default editor for PowerShell. In ISE you can run commands, write tests, and debug scripts in a graphical, window-based environment. It supports multi-line editing, syntax colouring, tab completion, selective execution, and more.

PowerShell ISE also offers a console pane that runs commands directly, while side panes display the source of your scripts and any tools plugged into ISE. You can open multiple script windows at once โ€” useful when debugging scripts that depend on functions defined in other scripts or modules.

PowerShell ISE

Recreate the earlier script in ISE:

  1. Paste the code into the editor.
  2. Save the script.
  3. Press F5 to run it.
  4. Observe output in the console pane.

Run script in ISE

Sample 2: the following script displays free virtual memory on the machine.

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

Free virtual memory script

PowerShell Concepts

The following concepts form the vocabulary of everyday PowerShell work.

Concept Description
Cmdlets Built-in commands written in .NET languages such as VB or C#. Developers extend the set by loading or writing PowerShell snap-ins.
Functions Commands written in the PowerShell language itself, without needing an IDE such as Visual Studio.
Scripts Text files on disk with the .ps1 extension.
Applications Existing Windows programs invoked from the shell.
WhatIf Tells the cmdlet not to execute, but to describe what would happen if it did.
Confirm Instructs the cmdlet to prompt before executing.
Verbose Provides a higher level of operational detail.
Debug Instructs the cmdlet to emit debugging information.
ErrorAction Defines what the cmdlet does on error. Allowed values: continue, stop, silentlycontinue, inquire.
ErrorVariable Names the variable that holds error information.
OutVariable Tells the cmdlet to store its output in a specific variable.
OutBuffer Holds a specific number of objects before sending them down the pipeline.

Advantages of Using PowerShell Scripts

  • PowerShell scripts are powerful and concise โ€” you can achieve a lot in just a few lines.
  • Variables are declared with the $<variable> syntax.
  • Variables can hold command output, objects, and primitive values.
  • The variable type does not need to be specified explicitly.

PowerShell vs. Command Prompt

PowerShell Command Prompt
Deeply integrated with Windows. Offers an interactive command line and full scripting language. Default command line interface provided by Microsoft. A simple Win32 application that talks to other Win32 objects.
Uses cmdlets that can be invoked interactively or through automation scripts. No cmdlets.
Treats output as objects so it can flow into another cmdlet through the pipeline. Output is a plain stream of text, not objects.
Advanced in features, capabilities, and internal architecture. Basic shell, suited to legacy batch tasks.

Applications of PowerShell

PowerShell has become the standard choice for IT administrators because it dramatically reduces effort across large corporate networks. Imagine managing more than four hundred servers and rolling out a new security solution that depends on a specific service running on every host.

Logging in to each server manually is slow and error-prone. A single PowerShell script can survey every server in minutes, report which hosts already run the service, and even enable it on the rest. The script captures intent once and applies it reliably across the estate.

FAQs

PowerShell automates Windows system administration โ€” managing services, files, users, and servers โ€” through an object-oriented command line and scripting language built on the .NET framework.

A cmdlet is a lightweight .NET class object that uses a verb-noun name and returns objects. A traditional command in other shells outputs plain text and handles its own parsing and formatting.

PowerShell works with objects and supports cmdlets, scripting, and deep .NET integration. Command Prompt is a basic text-based shell whose commands output plain text rather than objects.

A .ps1 file is a PowerShell script โ€” a text file containing one or more PowerShell commands. By default it cannot be run by double-clicking; it is executed from the console or with Run with PowerShell.

Open PowerShell as Administrator and run Set-ExecutionPolicy with a value such as RemoteSigned or Unrestricted, then confirm. Use Get-ExecutionPolicy to check the current setting.

The Integrated Scripting Environment (ISE) is PowerShell’s built-in GUI editor for writing, running, testing, and debugging scripts, with syntax coloring, tab completion, and multiple script tabs.

AI assistants suggest cmdlets, explain script behavior, and detect errors as you type. They help administrators automate tasks faster and convert plain-English requests into working PowerShell commands.

Yes. AI code assistants generate complete PowerShell scripts from a plain-English description, such as “list all stopped services and restart them,” and explain each cmdlet so you can review before running.

Summarize this post with: