🖥️
David Jenner's IT Notes
  • Command line and Powershell Tools
    • Troubleshoot with CMD
    • 🚀Troubleshoot with CMD & Powershell
    • 🔍Analysis
    • ⚡Automation
    • 🏃‍♂️Example Excerises
  • Microsoft Azure/Entra
    • Azure notes
Powered by GitBook
On this page
  • PowerShell Practice Notes
  • 🔹 Basic Notes
  • 🎓 Learning Tools
  • 🔹 About Cmdlets
  • 📝 Basic Cmdlets
  • 🔗 Piping Cmdlets
  • 🔹 Variables
  • 🔹 Arrays
  • 🔹 HashTables
  • 🏗️ Collecting User Input
  • 🔹 Conditional Statements
  • 🔹 Loops
  • 🔹 Functions
  • 🔹 Error Handling
  • 🔹 File Operations
  • 🔹 Active Directory (Admin Mode)
  • ⚡ Quick Tip

Was this helpful?

Edit on GitHub
  1. Command line and Powershell Tools

Example Excerises

PreviousAutomationNextAzure notes

Last updated 4 months ago

Was this helpful?

PowerShell Practice Notes


🔹 Basic Notes

  • ISE (Integrated Scripting Environment): A GUI for writing and debugging PowerShell scripts.

  • Everything in PowerShell is an OBJECT ✅

  • Check PowerShell version:

    $PSVersionTable.PSVersion
  • Ping a website:

    ping spacex.com

🎓 Learning Tools

  • Videos Used:


🔹 About Cmdlets

  • Cmdlet format: Verb-Noun (unless it's a custom function).

  • Check execution policy:

    Get-ExecutionPolicy
  • Change execution policy:

    Set-ExecutionPolicy RemoteSigned
  • RemoteSigned allows custom scripts to run.


📝 Basic Cmdlets

  • Print text:

    Write-Host "Hello World! " -NoNewline
    Write-Host "Hello Again!"
  • Find commands:

    Get-Command
    Get-Command -CommandType Cmdlet
  • Get help for a command:

    Get-Help Write-Host -Full

🔗 Piping Cmdlets

  • Write to a file:

    "May the force be with you!" | Out-File forcewithwho.txt
  • View file content:

    Get-Content .\forcewithwho.txt

🔹 Variables

  • Create a variable:

    $FavCharacter = "Spongebob"
  • Output a variable:

    Write-Output $FavCharacter
  • Save variable to a file:

    $FavCharacter | Out-File FavCharacter.txt
    Get-Content .\FavCharacter.txt
  • Find variable type:

    $FavCharacter.GetType()
  • List all properties:

    $FavCharacter | Select-Object -Property *
  • List all methods:

    Get-Member -InputObject $FavCharacter

🔹 Arrays

  • Define an array:

    $Jedi = @("Matthew","Mark","Luke","John")
  • Get first element:

    $Jedi[0]  # Arrays start at index 0
  • Add an element:

    $Jedi += "David"

🔹 HashTables

  • Define a hashtable:

    $Family = @{David = "Dad"; Steph ="Mum"; Charlotte ="Daughter"}
  • Add an entry:

    $Family.Add("Midnight","Cat")

🏗️ Collecting User Input

  • Prompt for user input:

    Write-Host "What is your favorite game system?"
    Write-Host "1. NES"
    Write-Host "2. Wii"
    Write-Host "3. N64"
    $FavSystem = Read-Host "Fav game system?"

🔹 Conditional Statements

  • If-Else Example:

    $PokemonCaught = 808
    If ($PokemonCaught -eq 908) {
        Write-Host "You're a Pokemon Master!"
    } Else {
        Write-Host "Go catch more Pokemon!"
    }
  • ElseIf Example:

    $PokemonNum = 200
    If($PokemonNum -ge 0 -and $PokemonNum -le 151) {
        Write-Host "Your Pokemon is from Kanto!"
    } ElseIf ($PokemonNum -ge 152 -and $PokemonNum -le 251) {
        Write-Host "Your Pokemon is from Johto!"
    }

🔹 Loops

  • For Loop:

    $Pets = @("cat","dog","fish","lizard","snake")
    For ($counter = 0; $counter -lt $Pets.Length; $counter++) {
        Write-Host "Hey, it's" $Pets[$counter]
    }
  • ForEach Loop:

    Foreach ($Pet in $Pets) {
        Write-Host $Pet "has arrived!"
    }
  • While Loop:

    $Office = @("Jim","Pam","Dwight","Michael")
    $counter = 0
    While ($counter -lt $Office.Length) {
        Write-Host $Office[$counter] "is going to the Mall! "
        $counter++
    }

🔹 Functions

  • Define a function:

    function Test-SpaceX {
        [CmdletBinding()]
        param(
            [Parameter(Mandatory)]
            [int32]$PingCount
        )
        Test-Connection spacex.com -Count $PingCount
    }
    Test-SpaceX -PingCount 3

🔹 Error Handling

  • Using Try-Catch:

    try {
        Test-SpaceX -PingCount 3
    } catch {
        Write-Output "Launch Problem"
        Write-Output $_
    }

🔹 File Operations

  • Create a file:

    New-Item -Path "C:\Users\YourName\Documents\ewok.txt" -ItemType "file" -Value "Praise C3PO!"
  • Move a file:

    Move-Item -Path "C:\Users\YourName\Documents\ewok.txt" -Destination "C:\Users\YourName\Documents\Scripts\"
  • Delete a file:

    Remove-Item .\ewok.txt

🔹 Active Directory (Admin Mode)

  • Import Active Directory Module:

    Import-Module ActiveDirectory
  • Get a user:

    Get-ADUser fbaggins
  • Create a new AD user:

    New-ADUser -Name "Luke Skywalker" -GivenName "Luke" -Surname "Skywalker" -SamAccountName "lskywalker" -UserPrincipalName "lskywalker@email.com"

⚡ Quick Tip

  • Use Clear-Host to clear the console screen.

  • Use Get-Process | Sort-Object WorkingSet64 -Descending | Select-Object -First 10 to find top 10 memory-consuming processes.


🏃‍♂️
PowerShell Tutorial