Certainly! Here’s a summary of some of the most important concepts in PowerShell, along with examples:
### 1. **Variables**
Variables store data and can be used to manipulate and work with that data.
```powershell
# Declaring a variable
$myVariable = "Hello, World!"
# Using a variable
Write-Host $myVariable
```
### 2. **Functions**
Functions allow you to encapsulate code into reusable blocks.
```powershell
function Get-Greeting {
param (
[string]$name
)
return "Hello, $name!"
}
# Calling the function
Write-Host (Get-Greeting -name "Alice")
```
### 3. **Cmdlets**
Cmdlets are specialized .NET classes that provide functionality for managing system administration tasks.
```powershell
# List all processes
Get-Process
# Stop a process by name
Stop-Process -Name notepad
```
### 4. **Pipelines**
Pipelines allow you to chain commands together, where the output of one command is the input to the next.
```powershell
Get-Process | Where-Object { $_.CPU -gt 0 } | Sort-Object CPU -Descending
```
### 5. **Conditional Statements**
Conditional statements help you control the flow of your script based on conditions.
```powershell
# If statement
if ($true) {
Write-Host "This will always print."
} elseif ($false) {
Write-Host "This will never print."
} else {
Write-Host "This will print if the first condition is false."
}
# Switch statement
switch ($true) {
{ $a -eq 1 } { Write-Host "a is 1"; break }
{ $a -eq 2 } { Write-Host "a is 2"; break }
default { Write-Host "a is neither 1 nor 2" }
}
```
### 6. **Loops**
Loops allow you to repeat actions.
```powershell
# For loop
for ($i = 0; $i -lt 5; $i++) {
Write-Host "Count: $i"
}
# While loop
$i = 0
while ($i -lt 5) {
Write-Host "Count: $i"
$i++
}
# Foreach loop
$items = @("apple", "banana", "cherry")
foreach ($item in $items) {
Write-Host $item
}
```
### 7. **Arrays and Hash Tables**
Arrays and hash tables are used to store collections of data.
```powershell
# Array
$numbers = @(1, 2, 3, 4, 5)
foreach ($number in $numbers) {
Write-Host $number
}
# Hash table
$hashTable = @{
"name" = "Alice"
"age" = 30
}
Write-Host $hashTable.name
```
### 8. **Error Handling**
Error handling allows you to catch and handle errors gracefully.
```powershell
try {
# Code that might throw an error
$result = 1 / 0
} catch {
Write-Host "An error occurred: $_"
} finally {
Write-Host "This will run regardless of whether an error occurred."
}
```
### 9. **Modules**
Modules are collections of cmdlets, functions, and scripts that extend the capabilities of PowerShell.
```powershell
# Import a module
Import-Module PSReadLine
# Use a cmdlet from the imported module
Get-Command -Module PSReadLine
```
### 10. **Script Files**
Scripts are text files containing PowerShell code that can be executed.
```powershell
# Save the following code in a file named 'example.ps1'
Write-Host "Running example.ps1"
# Execute the script
.\example.ps1
```
These concepts form the foundation of working with PowerShell effectively. Each concept has its own set of nuances and advanced features, but these basics should get you started.