Introduction to PowerShell
PowerShell is a powerful command-line shell and scripting language designed for automation and configuration management. Built on .NET, it provides deep system integration, making it a valuable tool for administrators and developers.
Why Use PowerShell?
- Automates repetitive tasks
- Manages system configurations
- Works with various data formats (JSON, XML, CSV)
- Supports remote administration
- Extensible via modules and scripts
Getting Started
Installation
PowerShell comes pre-installed on Windows, but newer versions (PowerShell Core) are cross-platform and can be installed on macOS and Linux.
Check PowerShell Version
$PSVersionTable.PSVersion
Install PowerShell Core
- Windows: Download from Microsoft Store (opens in a new tab)
- Linux/macOS: Use a package manager (e.g.,
brew install powershell
for macOS)
Running PowerShell
- Windows: Open
PowerShell
orpwsh
for PowerShell Core. - macOS/Linux: Run
pwsh
in a terminal.
Commands and Syntax
Cmdlets
PowerShell commands are called cmdlets and follow a Verb-Noun
format.
Get-Process # Retrieves running processes
Stop-Service # Stops a service
Finding Commands
Get-Command -Noun Process
Get-Help Get-Process -Full
Variables and Data Types
Declaring Variables
$Name = "PowerShell"
$Number = 10
$IsAdmin = $true
Data Types
[String]$Text = "Hello"
[Int]$Age = 30
[Array]$Items = @(1,2,3)
Operators
Arithmetic Operators
$Sum = 5 + 10
$Multiply = 4 * 3
Comparison Operators
$Result = 5 -eq 5 # True
$Check = 10 -gt 5 # True
Loops and Conditional Statements
If-Else
if ($Age -ge 18) {
Write-Output "Adult"
} else {
Write-Output "Minor"
}
Loops
# For Loop
for ($i=0; $i -lt 5; $i++) { Write-Output $i }
# While Loop
$counter = 1
while ($counter -le 3) {
Write-Output $counter
$counter++
}
Functions and Scripts
Creating Functions
function Greet {
param ($Name)
Write-Output "Hello, $Name!"
}
Greet -Name "User"
Running Scripts
Save as script.ps1
, then execute:
.\script.ps1
Modules and Packages
Importing Modules
Import-Module ActiveDirectory
Installing Modules
Install-Module -Name PowerShellGet
File System Operations
Working with Files
New-Item -Path "test.txt" -ItemType File
Get-Content "test.txt"
Working with Directories
New-Item -Path "C:\Temp" -ItemType Directory
Remove-Item -Path "C:\Temp" -Recurse
Working with JSON, XML, and CSV
JSON
$Data = Get-Content "data.json" | ConvertFrom-Json
$Data | ConvertTo-Json | Out-File "output.json"
XML
[xml]$xmlData = Get-Content "data.xml"
$xmlData.DocumentElement.ChildNodes
CSV
$csv = Import-Csv "data.csv"
$csv | Export-Csv "output.csv" -NoTypeInformation
Registry and WMI
Query Registry
Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion"
Query WMI
Get-WmiObject Win32_OperatingSystem
Remote Execution
Running Remote Commands
Invoke-Command -ComputerName "Server01" -ScriptBlock { Get-Service }
Entering Remote Sessions
Enter-PSSession -ComputerName "Server01"
Error Handling and Debugging
Try-Catch Blocks
try {
Get-Item "C:\NonExistentFile.txt"
} catch {
Write-Output "Error: $_"
}
Debugging Scripts
Set-PSDebug -Trace 1
Best Practices
General Guidelines
- Use meaningful variable names
- Follow
Verb-Noun
naming convention for functions - Use comments to improve readability
- Avoid hardcoding values; use parameters instead
Security Practices
- Avoid running scripts from untrusted sources
- Use
Set-ExecutionPolicy
to control script execution - Regularly update PowerShell modules
This guide provides a solid foundation in PowerShell. Keep exploring and practicing!