From Lab to Cloud – Scripting with PowerShell: The Fundamentals

From Lab to Cloud – Scripting with PowerShell: The Fundamentals

Time to shift the focus away..

Sections

Section 8.1 Introduction
Section 8.2 PowerShell - What is it and why we use it?
Section 8.3 PowerShell - Navigation and Basics
Section 8.4 PowersShell - Functions and conditions
Section 8.5 PowerShell - Integrating with AD DS and Azure
Section 8.6 PowerShell - Operators
Section 8.7 Final Words

Section 8.1 Introduction

The introduction to this blog and it is lesson 8. How time flies, we covered AD DS to Azure and the transition is beginning from lab to cloud. This lesson or blog how I call it will focus on a specific tool rather than resource. A tool that goes by the name of PowerShell.

PowerShell sounds like something powerful right?

That because this is a powerful tool that I (or if you are joining along, it will be we) will be using for this blog series. You can do a lot with this which I will discuss in this blog.


Section 8.2 PowerShell - What is it and why we use it?

But first things first, what is PowerShell? Let me break this down -

Microsoft definition of PowerShell is -

"PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework"

To me and you that sounds like gibberish and AI generated. But in simple terms PowerShell is a object oriented scripting language that uses a command line interface and is considered as a way to automate various tasks. Let me break this down into even more simpler definitions -

Object oriented - programming structure that focuses more on groups of information and data and the way they work with each other. Kind of like object groups.
Command line interface - CLI for short and is a text based interface where you type commands to and there is no graphical output.
Automate - using technology to perform tasks done by humans.

Why do we use it?

That is the most simplest definition I can give for what is PowerShell. Now begs the next question, why do we use this?

The answer to this question is simple and the answer is "automation". Technology will keep evolving day by day while as humans we will get lazier and lazier (controversial i know but that is my perception). PowerShell helps makes different tasks easier as it can be integrated with tools such as Active Directory, exchange, Azure and other tools to help with difficult tasks. The hardest bit is the scripting part but this blog will teach you about that in the lessons to come but for now it is about the basics.

That is the core fundamentals discussed, now onto the next section..


Section 8.3 PowerShell - Navigation and basics

I will break this section down to two headings. Let me talk now -

Heading 1 is navigation. Navigation in the dictionary is the process of getting from one place to the other. PowerShell acts as a navigator in some sense as you can use this to access different locations such as your downloads folder or user folder. Kind of like File explorer in different layout.

Now for some simple navigation commands

Command Purpose Example
D: or E: (Drive name) Access different drives on your system. Enter D: in PowerShell to switch to the D: drive.
cd Move through different directories. cd Users — moves into the Users folder.
cd \ Go back to the main directory (root). If in Users folder, running cd \ takes you back to the root of the drive.
Set-Location -Path <PATH> Navigate to a specified directory path. Set-Location -Path "C:\Program Files" — navigates to the Program Files folder.
Get-ChildItem -Path <PATH> List all files and folders in a specific directory. Get-ChildItem -Path "C:\Users\YourName\Downloads" — lists everything in the Downloads folder.

Basics

Now heading 2 will focus on more basic commands. The ones mentioned before are standard navigation commands. Now I will list basic commands which are ones you can try yourself and the ones that will be used often

Command Purpose Example
Get-Member Retrieve the properties and methods of an object. Get-Service | Get-Member
Get-Service Retrieve information about services running on the computer. Get-Service "wmi" — fetches the WMI service running.
Get-Process Get all the processes running on the computer. Get-Process winword, explorer | Format-List * — finds these processes and lists details.
Where-Object Select objects that match specific property criteria. Get-Process | Where-Object WorkingSet -GT 250MB — finds processes using more than 250MB of memory.
Get-Command Retrieve all available PowerShell commands. Get-Command — lists all commands available.
Get-Help <Command> -Online Get help for any PowerShell command you do not understand. Get-Help Get-Process -Online — opens the online help page for Get-Process.

Now that is the basics done, time to move onto the next section and focus on how to create functions and conditions.


Section 8.4 PowerShell - Function and conditions

Section 8.3 functions and conditions. Time to start from the beginning, what are functions and what are conditions?

Functions - block of code that is designed to perform a specific task. The code can be used be called and reused in different parts of the code. Each function can be called in different parts of code using the name of the function.
Conditions - A condition are expressions that evaluate to either true or false. An example of a condition would be an If statement or else statement. I will elaborate more on this further down the section.

Those two are simple definitions of what they are. Pretty easy definitions but now I will discuss them individually.

Functions

First header is functions. As mentioned in the definition above, "functions are a block of code designed to perform a specifc task".

What does that mean?

To explain this, I will demonstrate an example.

Example

function Get-Version {
    $PSVersionTable.PSVersion //Gets the PowerShell Version
}

# Line 1: function starts; name is Get-Version
function Get-Version {

    # Line 2: Retrieves the PowerShell version from $PSVersionTable
    $PSVersionTable.PSVersion

} # Line 3: Closing bracket; end of function

#Function is called
Get-Version 

This is an example of a function that has been created with a Line to Line breakdown what each line does. That is essentially what a function is, a block of code that does something.

Function in action

Functions in action

This is an example of how a function works in a working environment. I would recommend to open PowerShell ISE and create some test functions.

To create a function in PS you do function function_name { CODE HERE }
Simple way to create a function and then you call it by calling the function name.

Essentially, that is what a function is. Now onto conditions.

Conditions

Conditions as mentioned in the defintion are expressions that "evaluate to true or false".

What do I mean by that?

To better explain this, I will need to show an example of a condition that has been coded in PowerShell.

# Line 1: Condition variable is declared with the $ symbol and is set to true
$condition = $true

# Line 2: If statement created to check if the condition is true
if ( $condition )
{
    #Line 3: Outputs the sentence that the condition is true
    Write-Output "The condition was true"
}
# Line 3: else statement that is triggered if the if statement condition is false
else{
    # Line 4: Outputs the sentence that the condition was false
    Write-Output "The condition was false"
}  

This is a small snippet of code that shows how conditions tend to work. The simplest way to do this would be through the use of If/else statement.

The process works like this - If condition is true then run this block of code else if it is not true, run this block of code

Setting up conditions in PowerShell ISE

Conditions in action

Another example of how conditions work in PowerShell ISE. The if condition is met which means that the output "the condition was true" is outputted.

That is how that tends work in a simpler way. Kind of like a decision in some sense that has to be met. There is another way we can also check conditions and that is through the use of switch cases. But I will keep the conditions area simple for now and will attach the resources I have used to help me.

Sham's top 5 Resources 📚

  1. Microsoft Learn – PowerShell 101: Functions
    Official Microsoft guide explaining the basics of creating and using functions, including syntax, parameters, and return values.
  2. Codecademy – PowerShell Functions
    A quick reference that summarises function syntax and usage—perfect for quick look-ups while scripting.
  3. Netwrix Blog – How to Create a Function in PowerShell
    A practical, example-driven guide with code snippets showing how to build and call functions in real-world scenarios.
  4. Microsoft Learn - Everything you wanted to know about the if statement
    Microsoft guide that explains the basics of creating and using if conditions and how to use them
  5. Codeacademy – Conditionals
    Reference guide on how conditions work and how to set them up.

That was amazing, now onto section 8.4 where the fun carries on.


Section 8.5 PowerShell - Integrating with AD DS and Azure


Final section of this blog while contemplating if I should create a video of me explaining this concepts. Integrating with Active directory and Azure, two of the biggest concepts we covered in this home lab series.

Importing Azure modules

First things first, how will we get started. To get started we need to import some important modules. But first let me explain what modules are -

Modules - reusable unit that can contain different resources such as functions, cmdlets, providers and other resources

Now which modules to import - there are two important modules that will need to be imported to allow communication to happen between PS and AD and azure.

NOTE - The device will need to be connected to the domain to complete any actions

#Installing the module
Install-Module -Name ActiveDirectory
Install-Module -Name AzureAD

#Importing the module
Import-Module ActiveDirectory
Import-Module AzureAD

The example above shows how to install and import the modules for ActiveDirectory and AzureAD. The module needs to be installed first as PowerShell will need to store the scripts in a specific folder which can then be accessed by using the Import-Module command.

Example

Time for a simple example. I will demonistrate an example of a simple lockdown script that auto unlocks any accounts that are locked in Active Directory.

#IMPORTING THE MODULE
Import-Module ActiveDirectory

#Search-ADAccount finds all accounts which are locked out
$LockedOut = Search-ADAccount -LockedOut

#If there are no accounts locked out
if($LockedOut.Count -eq 0){
    Write-Host "No users are Locked out" -ForegroundColor Green

}
else{

foreach ($user in $LockedOut){
    $Output = Get-ADUser -Identity $LockedOut -Properties Name, UserPrincipalName
    Write-Output "Name:"$Output.Name"Email:"$Output.UserPrincipalName" "
    $Input1 =  Write-Host "Do you want to Unlock these accounts"
    if($Input1 -eq "Yes")
    {
        Search-ADaccount -Lockedout |Unlock-ADAccount
        Write-Host "$Output.Name has been unlocked"
    }

}

This is an example of a script that can be used to detect users that are locked out and then auto unlock them when you enter yes. I will break down some key commands below

Command Purpose Example
Import-Module ActiveDirectory Imports the Active Directory module to enable AD-related commands in PowerShell. Import-Module ActiveDirectory
Search-ADAccount -LockedOut Finds all Active Directory accounts that are locked out. Search-ADAccount -LockedOut
Get-ADUser -Identity <UserName> Retrieve the details of a specific AD user (e.g., a locked-out account). Get-ADUser -Identity "jsmith"
Unlock-ADAccount Unlocks a locked-out Active Directory user account. Unlock-ADAccount -Identity "jsmith"

This is an example script that tends to show you the basics. I normally practice these on a domain joined device using PowerShell ISE. You can use the DC VM to also practice these commands and find more scripts to do online.


Section 8.6 PowerShell - Operators

A bonus section in this blog and an important piece of knowledge to have when using PowerShell.

First what is the definition of operators?

Operators are characters in expressions that you can use in commands to perform certain actions. A short definition on what an operator is. There are different types of operators and they are arithmetic operator, comparison operator and logical operator.

Arithmetic operators

The first type of operator is Arithmetic operator and these are mathematical expressions that are used in equations. I will demonstrate all these operators in the table below.

Operator Name Description
+ Addition Adds the two values provided on either side. Example: 5 + 38
- Subtraction Subtracts the right-hand number from the left-hand number. Example: 10 - 46
* Multiplication Multiplies the values on either side. Example: 7 * 214
/ Division Divides the left-hand value by the right-hand value. Example: 20 / 54

Comparison operators

These operators compare two numbers or alphabetical values and returns true if the condition is met. The symbols are listed below -

Operator Name Description
-eq Equal Checks if two values are equal. Example: if (A -eq B)
-ne Not Equal Checks if two values are not equal. Example: if (A -ne B)
-gt Greater Than Checks if the first value is greater than the second value. Example: if (A -gt B)
-ge Greater Than or Equal Checks if the first value is greater than or equal to the second value. Example: if (A -ge B)
-lt Less Than Checks if the first value is less than the second value. Example: if (A -lt B)
-le Less Than or Equal Checks if the first value is less than or equal to the second value. Example: if (A -le B)

These are the main two operators you will see a lot in PowerShell scripts. There are more that can be discussed but I will keep it short for this section as we have covered a lot. But I would recommend checking out Sham's top 5 resources for websites with more content.


Section 8.7 Final words

Look at the time, it is now the end of the blog. A whole whirlwind of content discussed with a lot of examples which should hopefully master the fundamentals of PowerShell.

What now?

The best thing I can advise is to pick some PowerShell projects and experiment in your DC VM with different commands and modules. There is a whole lot you can do with this.

As a wise man once said "The journey is not dictated by others but it is something you start and build"

-Shamas