PowerShell Scripting Basics

Welcome to the foundational guide for PowerShell scripting. This article will walk you through the essential concepts and commands needed to start automating tasks on Windows and beyond with PowerShell.

What is PowerShell?

PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and an associated scripting language. Built on the .NET framework, PowerShell uses cmdlets (pronounced "command-lets") to perform specific actions. Unlike traditional shells that return text, PowerShell cmdlets return objects, which makes scripting more powerful and flexible.

Your First PowerShell Commands

Let's start with some basic commands to get you familiar with the environment.

Getting Help

The first command every scripter should know is how to get help:

Get-Help Get-Command

To get more detailed help on a specific command:

Get-Help Get-Command -Full

You can also find commands related to a topic:

Get-Command -Noun Process

Working with Objects

PowerShell cmdlets return objects. Let's see an example. The Get-Process cmdlet retrieves a list of running processes on the local computer.

Get-Process

You can select specific properties of these objects:

Get-Process | Select-Object Name, Id, CPU

Pipelines

The pipe symbol (|) is crucial in PowerShell. It allows you to pass the output of one cmdlet as input to another. This is how you chain commands together to create more complex operations.

Get-Process | Where-Object {$_.CPU -gt 100} | Sort-Object CPU -Descending

In this example:

  • Get-Process retrieves all running processes.
  • Where-Object {$_.CPU -gt 100} filters those processes where the CPU usage is greater than 100. $_ represents the current object in the pipeline.
  • Sort-Object CPU -Descending sorts the filtered processes by CPU usage in descending order.

Basic Scripting Concepts

Variables

Variables are used to store data. They start with a dollar sign ($).

$serverName = "MyServer01"
$portNumber = 80
$errorMessage = "Connection failed."

Conditional Statements

Use If, ElseIf, and Else to control the flow of your script.

$fileSize = Get-Item "C:\path\to\your\file.txt" | Select-Object -ExpandProperty Length
if ($fileSize -gt 1MB) {
    Write-Host "File is large."
} else {
    Write-Host "File is small."
}

Loops

Loops allow you to repeat a block of code.

# ForEach loop
$servers = "Server1", "Server2", "Server3"
foreach ($server in $servers) {
    Write-Host "Checking status of $server..."
    # ... your commands here ...
}

# For loop
for ($i = 0; $i -lt 5; $i++) {
    Write-Host "Iteration number: $i"
}

Creating Your First Script File

You can write PowerShell scripts in any text editor, but it's recommended to use an Integrated Scripting Environment (ISE) like the PowerShell ISE or Visual Studio Code with the PowerShell extension.

  1. Open PowerShell ISE or VS Code.
  2. Create a new file.
  3. Type your commands.
  4. Save the file with a .ps1 extension (e.g., MyScript.ps1).

To run a script, navigate to its directory in PowerShell and type its path:

.\MyScript.ps1

Execution Policy

By default, running scripts might be restricted. You might need to adjust the execution policy. Use Get-ExecutionPolicy to check and Set-ExecutionPolicy RemoteSigned (or another suitable policy) to change it. Be aware of the security implications.

Next Steps

This article covers the very basics. To delve deeper, explore concepts like:

  • Functions
  • Error Handling (Try-Catch)
  • Modules
  • Working with the File System
  • Remoting
  • Desired State Configuration (DSC)

The PowerShell community is vast and helpful. Don't hesitate to search online forums and Microsoft's official documentation for more advanced topics and real-world examples.