Understanding PowerShell for Beginners

Dec 4, 2024

Introduction to PowerShell

Overview

  • PowerShell video series: From basics to mastery.
  • Assumes prior knowledge of command lines (e.g., cd, ls).
  • Built on .NET framework.

PowerShell Setup

  • Windows comes with an older version (v5).
  • Recommended to download the latest version from GitHub for various platforms.

PowerShell Basics

Objects vs Text

  • Commands return objects instead of text.
  • Objects contain properties (e.g., file name, path, extension).
  • Powershell displays objects in a readable text format (table or list).

Object Manipulation

  • Use pipes | to pass objects between commands.
  • Format-List: Lists object properties.
  • Format-Table: Displays objects in table view.

Key Commands

Get-Process

  • Retrieves all running processes.
  • Displays in table view by default.

Command Analysis

  • Properties like Name, ID, etc., shown along top of tables.
  • Use Format-List to view object properties in list format.

Measure

  • Analyzes objects (e.g., counts them).
  • Can calculate statistics like average, sum.

Advanced PowerShell Usage

Where

  • Filters objects based on conditions.
  • Syntax: Where {condition}.
  • Conditions use $_.property -eq 'value'.

Sort

  • Orders objects by specified properties.
  • Syntax: Sort-Object {property}.

ForEach

  • Executes actions for each object.
  • Can filter down to specific properties.
  • Syntax: ForEach-Object {action}.

Practical Example

  • Get processes with ID > 4000, calculate average CPU time.
    1. Use Get-Process.
    2. Filter IDs using Where.
    3. Extract CPU property with ForEach.
    4. Calculate average with Measure.

Conclusion

  • Covered objects, pipes, and key commands: Measure, Where, Sort, ForEach.
  • Next video: Deep dive into command parameters, aliases, and discovery.