PowerShell Scripting

PowerShell ISE

Though we've mentioned it before, let's reiterate for clarity.

Windows PowerShell ISE, short for Windows PowerShell Integrated Scripting Environment, is an application that provides a graphical user interface (GUI) for PowerShell. This interface allows you to write, execute, test, and debug commands all within a single window.

Writing Scripts with PowerShell

PowerShell scripts are text files that contain a series of commands and functions and typically have a .ps1 extension.

They are used to automate tasks involving:

  • System management: Automating tasks such as creating user accounts, formatting disks, copying, and deleting files.

  • Network management: Automating tasks such as configuring network connections, managing IP addresses, and accessing remote servers.

  • Data processing: Automating tasks such as analyzing data, generating reports, and interacting with databases.

  • Software development: Automating tasks such as deploying applications, test automation, and debugging.

Control Structures

Variables

Variables are used to store and manipulate data in your scripts.

# Define a variable
$name = "John Doe"

# Assign a value to a variable
$age = 30

# Retrieve the value of a variable
$name

# Define multiple variables in a single line
$firstName, $lastName = "John", "Doe"

Conditions

Conditions allow you to determine when a specific action should be performed.

If-Else Structure

if ($age -gt 18) {  
    Write-Host "Adult"
} else {  
    Write-Host "Kid"
}

Switch-Case Structure

$dayOfWeek = Get-Date -Format dddd  # Get the current day of the week in full string format (e.g., "Monday", "Tuesday", etc.)

switch ($dayOfWeek) {    
    "Monday" { Write-Host "Today is Monday. Time to start the week!" }    
    "Tuesday" { Write-Host "Tuesday! Still a long way to go!" }    
    "Wednesday" { Write-Host "It's Wednesday. Halfway through!" }    
    "Thursday" { Write-Host "Thursday! Almost there!" }    
    "Friday" { Write-Host "Friday! Time to celebrate the weekend!" }    
    "Saturday" { Write-Host "Saturday! Enjoy the weekend!" }    
    "Sunday" { Write-Host "Sunday! Last day before Monday!" }    
    Default { Write-Host "Invalid day of the week." }
}

Loops

Loops allow you to repeat a block of code a certain number of times or until a condition is met.

For Loop

for ($i = 1; $i -le 10; $i++) {  
    Write-Host $i
}

While Loop

$i = 1
while ($i -le 10) {  
    Write-Host $i  
    $i++
}

Do-While Loop

$i = 1
do {  
    Write-Host $i  
    $i++
} while ($i -le 10)

Foreach Loop

$names = "John", "Jane", "Doe"
foreach ($name in $names) {  
    Write-Host $name
}

Writing and Saving

Open the PowerShell ISE program, write your commands, and save them.

The PowerShell Gallery is a central repository for sharing and obtaining PowerShell scripts. It contains code from both Microsoft and the community. Through the PowerShellGet module, you can install scripts directly from PowerShell, but be cautious about the accuracy and security of the code since community-created content can also be found.

For example, to search for the Sysinternals module, you can use

Find-Module.

PS C:\Users\Administrator> Find-Module -Name "sysinternals"
Version    Name                                Repository           Description
-------    ----                                ----------           -----------
1.1.0      SysInternals                        PSGallery            PowerShell cmdlets for SysInternal 
tools

If you are searching for the first time, it will prompt you to install NuGet. Press Enter to download.

Installation

Install-Module -Name SysInternals

Last updated