πββοΈExample Excerises
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: PowerShell Tutorial
πΉ 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 "[email protected]"
β‘ 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.
Last updated
Was this helpful?