Getting Started with PowerShell

Welcome to the Getting Started guide for PowerShell! This section will walk you through the essential steps to begin using PowerShell for automation, configuration management, and command-line tasks.

1. 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. It is designed for system administrators and power users to control and automate administration across Windows and other platforms.

2. Installing PowerShell

Windows

PowerShell is pre-installed on modern Windows versions. To check which version you have:

Get-Host | Select-Object Version
            

If you need the latest version, you can download it from the official Microsoft PowerShell documentation.

macOS and Linux

PowerShell Core (PowerShell 7 and later) is cross-platform. Installation instructions for various Linux distributions and macOS can be found in the official Microsoft PowerShell documentation.

3. Launching PowerShell

You can launch PowerShell in several ways:

4. Your First PowerShell Commands

Once PowerShell is open, you'll see a prompt (e.g., PS C:\Users\YourUsername>). Let's try some basic commands:

Getting Help

The Get-Help cmdlet is your best friend. To get help on any command, use:

Get-Help Get-Command
            Get-Help Get-Help -Full
            

Exploring Your System

PowerShell uses cmdlets (command-lets) which typically follow a Verb-Noun structure.

Understanding Objects

Unlike traditional shells that deal with plain text, PowerShell treats everything as an object. This makes filtering and manipulating data much more powerful.

For example, to see only the names of running processes:

Get-Process | Select-Object Name
            
Note: The pipe character (|) passes the output object from one cmdlet to another.

5. Basic Scripting

You can write PowerShell scripts in plain text files with a .ps1 extension. Let's create a simple script.

Create a Script File

  1. Open a text editor (like VS Code with the PowerShell extension, or Notepad).
  2. Enter the following content:
  3. # My First PowerShell Script
                    Write-Host "Hello, PowerShell World!"
                    Get-Date
                    
  4. Save the file as MyScript.ps1 in a convenient location (e.g., your Documents folder).

Running a Script

By default, PowerShell has a script execution policy that might prevent you from running scripts. To run your script, you may need to navigate to its directory in PowerShell and execute it:

cd C:\Path\To\Your\Script
            .\MyScript.ps1
            
Execution Policy: If you encounter an error about script execution being disabled, you might need to adjust your execution policy. Use Get-ExecutionPolicy to check and Set-ExecutionPolicy RemoteSigned -Scope CurrentUser to change it (run PowerShell as Administrator if changing for the system). Be aware of the security implications.

6. Next Steps

Congratulations! You've taken your first steps into PowerShell. Here are some recommendations for continuing your learning journey: