Home WINDOWS Windows PowerShell Scripting Tutorial for Beginners

Windows PowerShell Scripting Tutorial for Beginners

0
Windows PowerShell Scripting Tutorial for Beginners

[ad_1]

Whether you are a technology enthusiast or a professional looking to enhance your scripting skills, we have designed this Windows PowerShell scripting tutorial for beginners, especially for you. So, if you have no prior knowledge  of PowerShell scripting, this post will start from the basics.

Windows PowerShell scripting tutorial for beginners

We will walk you through the installation process, help you understand the PowerShell environment, and gradually introduce you to the core concepts and features of PowerShell scripting. So, if you are ready to learn PowerShell scripting, let’s get started on this empowering journey together!

What is Windows PowerShell?

Developed by Microsoft, PowerShell is a mix of command-line shell and scripting language. It enables users to automate tasks and manage complex system configurations more efficiently. With command-line processing, scripting capabilities, and system administration tools, PowerShell provides a seamless and robust platform for managing Windows operating systems.

Features and applications of PowerShell

Therefore, with PowerShell you can:

  • Develop with cmdlets.
  • Automate tasks.
  • Manage Windows OS devices remotely using PowerShell scripts and cmdlets.
  • Access all types of .NET frameworks.
  • Listen, forward and act on events related to management
  • Control the server and workstation components with its uncomplicated syntax.
  • Share data across applications.
  • Get built-in support for file transfer between devices using BITS (Background Intelligence Transfer Service).
  • Run script jobs in the background either on a local device or more than one remote device.
  • Script securely compared to VBScript or other scripting languages.

To learn more, refer to our detailed post on what is Windows PowerShell and the features and benefits of upgrading to the latest version.

Windows PowerShell Scripting Tutorial for Beginners

So, if you are the system admin, you can take advantage of PowerShell to solve problems efficiently and prevent investing time in manual work in the future. That said, by the end of this Windows PowerShell scripting tutorial for beginners, you will not just learn PowerShell scripting, but also have a solid foundation.

  1. How to launch PowerShell?
  2. Verify script execution policy settings
  3. What is PowerShell Cmdlet?
  4. Difference between Cmdlet and Command
  5. What is PowerShell scripting?
  6. What are the components of a PowerShell script?
  7. Run the PowerShell script

1] How to launch PowerShell?

Windows PowerShell scripting tutorial for beginners

PowerShell is pre-installed in Windows and hence, you do not need to download and install it separately. So, here’s how to launch PowerShell:

Right-click on Start and select Windows Terminal from the menu.

This will open the PowerShell Window.

To open PowerShell with admin rights, right-click on Start and select Windows Terminal (Admin).

You can now run your cmdlets or commands in the PowerShell.

Read: How to automate tasks with PowerShell

2] Verify script execution policy settings

Windows PowerShell scripting tutorial for beginners

Running scripts in PowerShell are automatically restricted for security concerns. Hence, we recommend you verify the the execution policy for running scripts in PowerShell. To check the PowerShell execution policy, you must create and run the PowerShell script file.

3] What is PowerShell Cmdlet?

A cmdlet is a short and lightweight command that performs a specific action in PowerShell. Cmdlets form the fundamental blocks of PowerShell scripts and commands that follow a consistent syntax. Written in .NET, they are designed to perform a single function using short commands like Get-Process, Stop-Service, or New-Item. So, these are more like an order to initiate something.

Read: How to schedule PowerShell script using Task Scheduler

4] Difference between Cmdlet and Command

That said, to learn PowerShell scripting, you must know the difference between cmdlets and commands. Cmdlets are different from commands in other command-shell environments. Here’s how:

  • Cmdlets are .NET Framework class objects so, you cannot run these commands separately.
  • Cmdlets can be created easily since these are shorter compared to commands.
  • Unlike PowerShell, cmdlets do not handle error presentation, Parsing, or output formatting.
  • Since cmdlets are known to work on objects, and not on text streams and objects.
  • Cmdlets process only one object at a time since they are record-based.

For PowerShell, the following are the differences:

  • While most of the commands contain cmdlets, there are also other types of commands such as functions, aliases, or external executable programs.
  • Cmdlets are a specific type of command in PowerShell, but commands can encompass a wider range of actions and functionalities.

So, in short, a cmdlet is a command in PowerShell, but not all commands in PowerShell are necessarily cmdlets.

5] What is PowerShell scripting?

Windows PowerShell scripting tutorial for beginners

Now to automate the administrative tasks, you will form statements in PowerShell using cmdlets, functions, variables, etc., and this process is called scripting. This is the PowerShell language that helps you to automate administrative tasks. To fulfill the tasks, you create steps and these steps are further stored in a file with the .ps1 extension. This file is the script that you need to run.

So, below are the components that form the steps of a script.

Read: How to turn on or off Windows PowerShell script execution

6] What are the components of a PowerShell script?

PowerShell Commands list

  • Get-Process – Pulls up the details related to the running processes of the system.
  • Get-Service – Gets the current status of services on the system.
  • Get-ChildItem – Pulls up the entire list of files and folders in a directory.

PowerShell data types

Some of the usual PowerShell data types include:

  • String – A series of characters within single quotes (‘ ‘) or double quotes (” “) which could be letters, numbers, symbols, and spaces.
  • Integer – These are whole numbers minus the decimal or fractional parts.
  • Double – These are floating-point numbers with decimal precision. For example, 2.15 or -0.2.
  • Boolean – These are commonly used in conditional expressions and comparisons, with possible values as either true or false.
  • Hashtable – It’s a cluster of key-value pairs where each key must be unique like a password. These are mostly used to store and retrieve data using named keys.
  • Array – It’s a group of ordered and indexed elements that belong to the same data type. For example, a set of names or integers. To create an array in PowerShell,  you must assign a list of values separated with commas and enclosed in parentheses to a variable, as shown below:
$colors = ("red", "green", "blue")

To access each of the elements of an array separately, you can use the index operator [n]. As a rule, the index of the first element in the array is always 0. Please refer to the example below:

$colors = ("red", "green", "blue")

Write-Host $colors[0] # Output from first element: red

Write-Host $colors[1] # Output from second element: green

Write-Host $colors[2] # Output from third element: blue

You can also assign a new value to the index as follows:

$colors = ("red", "green", "blue")


$colors[1] = "white"


Write-Host $colors[1] # Output: white

PowerShell Variables

These are arguments to commands and are used to store values.

A variable name starts with $ and then follows the name of the variable. Please note that variable names are not case-sensitive, for example, you can write both $colors or $Colors and it won’t matter.

To assign a value to the variable, you must use the = symbol, as shown below:

  • $Color = "Red"
  • $number = 20

PowerShell Pipes

The PowerShell pipe (also called a pipeline) is an operator or symbol |, that passes over the output of one cmdlet to another. This helps in creating a single-line command useful for complex tasks.

Below is an example that helps retrieve all the system services and classifies them based on Status:

Get-Service | Sort-Object -Property Status

PowerShell Operators

Operators

Symbols

Purpose

Arithmetic Operators +, -, *, /, % Calculate numeric values
Assignment Operators %=, +=, -=, =, *=, /= Helps to assign, change, or change values to variables
Comparison Operators -le, -ne, -gt, -lt, -eq, -ge Binary operators that compare two integer or string values that return True/False based on whether the operator condition is met or not.
Logical Operators -or, -xor, -and, -not, ! Based on Boolean values, these add up multiple operator expressions and statements into complex conditionals. They return output in Boolean values.
Redirection Operators >>, >, 2>>, 2>, and 2>&1 Sends the output of a command or expression to a text file.
Split and Join Operators -Split, -Join Divides and adds substrings.
Type Operators -is not, -is, -as Finds or amend the .NET Framework type of an object.
Unary Operators ++ (Increment), — (Decrement) Increase or decrease the value of a variable by 1

To find details about any cmdlet, you can use the below command:

Get help for cmdlets

You can refer to our detailed post on basic PowerShell commands to know about the most commonly used commands.

7] Run the PowerShell script

Windows PowerShell scripting tutorial for beginners

While you can use the Notepad to create a script and then call it from PowerShell, it’s always better to use the PowerShell Integrated Scripting Environment (ISE).

While the PowerShell ISE app comes pre-installed in Windows 11, if you do not find it on your computer, you can install PowerShell ISE through Optional features.

To start PowerShell ISE, open the elevated Windows PowerShell, type in the below command and hit Enter:

powershell_ise.exe

You can get more information related to the PowerShell ISE through its official Microsoft page.

To learn PowerShell scripting, here’s how you can create and run PowerShell script file. We have also specified a sample script below that you can run in the PowerShell window:

# Prompt the user for their name

$name = Read-Host "Enter your name"

# Greet the user

Write-Host "Hello, $name! Welcome to PowerShell scripting."

Once you enter the script, click on the green arrow icon in the menu bar to Run the script or press F5.

Next, type in your name next to Enter your name: field and hit Enter.

It will generate the greetings as below:

Hello, Madhu! Welcome to PowerShell scripting.

Read: Windows PowerShell ISE vs Windows PowerShell: What is the difference?

How can I learn PowerShell scripting fast?

To learn PowerShell scripting quickly, understand command-line interface basics and common cmdlets. It’s useful to have a grasp of scripting fundamentals and Windows administration. Prioritize hands-on practice, use online tutorials, engage with PowerShell communities, and frequently write and test small scripts to build your skills efficiently.

Windows PowerShell scripting tutorial for beginners

[ad_2]

Source link

www.thewindowsclub.com

LEAVE A REPLY

Please enter your comment!
Please enter your name here