CodeToLive

PowerShell Introduction

PowerShell is a cross-platform task automation and configuration management framework from Microsoft, consisting of a command-line shell and scripting language.

What is PowerShell?

PowerShell is built on the .NET Common Language Runtime (CLR) and provides full access to COM, WMI, and .NET frameworks. It was designed specifically for system administrators to automate tasks and manage configurations.

Key Features:

  • Object-oriented pipeline (not just text)
  • Extensible through functions, classes, modules
  • Consistent command naming (Verb-Noun pattern)
  • Powerful scripting capabilities
  • Cross-platform (Windows, Linux, macOS)

Getting Started

To launch PowerShell:

  • Windows: Search for "PowerShell" in Start menu
  • macOS/Linux: Install via package manager and run pwsh

Basic Commands

# Get help about commands
Get-Help Get-Process

# List all running processes
Get-Process

# Get services
Get-Service

# List directory contents
Get-ChildItem

# Get command history
Get-History

PowerShell Versions

Version Release Year Key Features
PowerShell 1.0 2006 Initial release
PowerShell 5.1 2016 Last Windows-only version
PowerShell 7+ 2020 Cross-platform, .NET Core based

PowerShell ISE vs VS Code

While PowerShell ISE was the traditional editor, Microsoft now recommends using Visual Studio Code with the PowerShell extension:

# Install PowerShell extension in VS Code
code --install-extension ms-vscode.PowerShell

Execution Policy

PowerShell scripts (.ps1 files) have execution restrictions for security:

# Check current execution policy
Get-ExecutionPolicy

# Set execution policy (admin required)
Set-ExecutionPolicy RemoteSigned

Basic Syntax Examples

# Variables
$name = "PowerShell"
$version = 7.2

# Output
Write-Host "Hello, $name $version!"

# If statement
if ($version -ge 7) {
    Write-Host "You're running the latest version"
} else {
    Write-Host "Consider upgrading"
}

# For loop
for ($i = 1; $i -le 5; $i++) {
    Write-Host "Counting: $i"
}

# Function
function Get-Greeting {
    param($name)
    return "Hello, $name!"
}

Get-Greeting -name "Admin"