PowerShell Basics
Summary
Essential information for getting started with PowerShell including versions, environments, and execution policies.
What is PowerShell?
PowerShell is a task automation and configuration management framework from Microsoft. It consists of:
- A command-line shell for running commands
- A scripting language for automation
- A framework for managing systems
PowerShell vs CMD vs Bash
# PowerShell - Works with objects
Get-Process | Where-Object {$_.CPU -gt 100} | Select-Object Name, CPU
# CMD - Works with text
dir | find "txt"
# Bash - Works with text
ls | grep "txt"
Key Differences:
- PowerShell: Object-oriented, structured data, powerful pipeline
- CMD: Text-based, limited functionality, legacy Windows
- Bash: Text-based, powerful in Linux/Unix, text parsing required
Why PowerShell?
PowerShell works with objects, not text. This means you get structured data with properties and methods, making automation much easier than parsing text output.
PowerShell Versions
Windows PowerShell (5.1)
- Built into Windows 10/11 and Windows Server
- Based on .NET Framework
- Windows-only
- This is what you have by default on Windows
PowerShell 7+ (Cross-Platform)
- Open-source, cross-platform (Windows, Linux, macOS)
- Based on .NET Core/.NET 6+
- Faster, more features
- Installed separately (doesn't replace 5.1)
Which Version Should I Use?
- Learning? Either works, but 7+ is recommended for new features
- Work environment? Use what's installed (usually 5.1)
- Personal projects? Install PowerShell 7+ for latest features
To install PowerShell 7+:
PowerShell Environments
PowerShell Console (Default)
The basic blue PowerShell window:
- Built into Windows
- Limited features
- Works but not ideal for development
How to open:
- Start Menu → Search "PowerShell"
Win + X→ "Windows PowerShell"
Windows PowerShell ISE
Integrated Scripting Environment - comes with Windows:
- Script editor + console
- Syntax highlighting
- IntelliSense (autocomplete)
- Debugging tools
- Only supports PowerShell 5.1 (not 7+)
How to open:
- Start Menu → Search "PowerShell ISE"
ISE is Legacy
ISE is deprecated and won't get new features. It's fine for quick scripts but VS Code is recommended for serious work.
Visual Studio Code (Recommended)
Modern, powerful code editor:
- Works with PowerShell 5.1 and 7+
- Excellent PowerShell extension
- Git integration
- Best IntelliSense
- Debugging
- Integrated terminal
Setup: 1. Install VS Code: https://code.visualstudio.com 2. Install PowerShell extension (by Microsoft) 3. Configure integrated terminal to use PowerShell
Windows Terminal (Best Console)
Modern terminal app from Microsoft:
- Tabs, panes, beautiful themes
- Works with PowerShell, CMD, WSL, etc.
- Unicode support
- GPU-accelerated
To install:
Recommended Setup
For beginners: Windows Terminal + PowerShell 7 For scripting: VS Code + PowerShell extension For quick commands: Windows Terminal
Execution Policies
Execution policies control whether PowerShell scripts can run. This is a safety feature, not security.
Policy Levels
# Check current execution policy
Get-ExecutionPolicy
# Common policies:
Restricted # No scripts allowed (default on some systems)
AllSigned # Only signed scripts
RemoteSigned # Local scripts OK, remote must be signed (recommended)
Unrestricted # All scripts allowed (prompts for remote)
Bypass # Nothing blocked, no warnings
Setting Execution Policy
# Set for current user (recommended, doesn't need admin)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Set for entire machine (needs admin)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
# Temporarily bypass for one session
PowerShell.exe -ExecutionPolicy Bypass -File "C:\Scripts\myscript.ps1"
Common Beginner Issue
If you get "cannot be loaded because running scripts is disabled", you need to set execution policy:
Understanding Remote Scripts
When you download a script from the internet, Windows marks it as "from another computer":
# Check if file is blocked
Get-Item C:\Scripts\downloaded.ps1 | Get-Item -Stream Zone.Identifier
# Unblock a downloaded script
Unblock-File -Path C:\Scripts\downloaded.ps1
# Unblock all scripts in a folder
Get-ChildItem -Path C:\Scripts -Recurse | Unblock-File
Running PowerShell Scripts
Method 1: From PowerShell Console
# Navigate to script folder
cd C:\Scripts
# Run script (note the .\ prefix)
.\myscript.ps1
# Or use full path
C:\Scripts\myscript.ps1
# Run with parameters
.\myscript.ps1 -Name "John" -Age 30
Must Use .\ or Full Path
PowerShell doesn't run scripts from current directory without.\ for security. Method 2: Right-Click Context Menu
Right-click a .ps1 file:
- "Run with PowerShell" - Runs and closes immediately
- "Edit" - Opens in ISE (if installed)
Right-Click Issues
"Run with PowerShell" closes the window immediately. Add Read-Host "Press Enter to exit" at the end of your script to keep it open.
Method 3: From CMD or Run Dialog
powershell.exe -File "C:\Scripts\myscript.ps1"
REM Or execute command directly
powershell.exe -Command "Get-Process | Where-Object {$_.CPU -gt 100}"
Method 4: Scheduled Tasks
Create a scheduled task:
- Program:
powershell.exe - Arguments:
-ExecutionPolicy Bypass -File "C:\Scripts\myscript.ps1"
PowerShell Profile
Your profile is a script that runs automatically when PowerShell starts. Use it for customization:
# Check if profile exists
Test-Path $PROFILE
# Create profile if it doesn't exist
if (!(Test-Path $PROFILE)) {
New-Item -Path $PROFILE -ItemType File -Force
}
# Edit profile
notepad $PROFILE
# Or in VS Code:
code $PROFILE
Common Profile Customizations
# Example profile contents
# Custom prompt
function prompt {
"PS [$env:USERNAME@$env:COMPUTERNAME] $(Get-Location)> "
}
# Aliases
Set-Alias -Name np -Value notepad
Set-Alias -Name ll -Value Get-ChildItem
# Custom functions
function Get-MyIP {
(Invoke-WebRequest -Uri "https://api.ipify.org").Content
}
# Set default location
Set-Location C:\Scripts
# Load modules
Import-Module MyCustomModule
# Display welcome message
Write-Host "Welcome back, $env:USERNAME!" -ForegroundColor Green
Profile Locations
# Current user, current host
$PROFILE
# Current user, all hosts
$PROFILE.CurrentUserAllHosts
# All users, current host
$PROFILE.AllUsersCurrentHost
# All users, all hosts
$PROFILE.AllUsersAllHosts
PowerShell Basics - Quick Start
Starting Your First Session
# Check version
$PSVersionTable.PSVersion
# Get current location
Get-Location
# List files
Get-ChildItem
# Change directory
Set-Location C:\Temp
# Clear screen
Clear-Host
# Or just: cls
# Get command history
Get-History
# Exit PowerShell
exit
Tab Completion
PowerShell has excellent tab completion:
# Type partial command and press Tab
Get-Pro[Tab] # Cycles through Get-Process, Get-ProvisioningPackage, etc.
# Tab complete parameters
Get-Process -[Tab] # Cycles through -Name, -Id, -ComputerName, etc.
# Tab complete file paths
cd C:\Pro[Tab] # Expands to C:\Program Files\
# Ctrl+Space shows all options
Get-Process [Ctrl+Space] # Shows all available parameters
Command History
# Use arrow keys
↑ (Up Arrow) # Previous command
↓ (Down Arrow) # Next command
# Search history
Ctrl+R # Reverse search (type to search)
# Get history
Get-History
# Run command from history
Invoke-History 5 # Runs command #5 from history
# Run last command
!! # In PowerShell 7+
Common Beginner Mistakes
Mistake 1: Not Setting Execution Policy
# Error: "cannot be loaded because running scripts is disabled"
# Fix:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Mistake 2: Forgetting .\ When Running Scripts
Mistake 3: Using Wrong Operators
# Wrong (from other languages):
if ($x == 5) { } # Error!
if ($x > 5) { } # Error!
# Right (PowerShell way):
if ($x -eq 5) { } # Correct
if ($x -gt 5) { } # Correct
Mistake 4: Not Checking Variable Existence
# Dangerous:
$result = $myVariable.Property # Error if $myVariable is null
# Safe:
if ($null -ne $myVariable) {
$result = $myVariable.Property
}
Tips & Tricks
Copy/Paste in Console
Get Command Path
PowerShell Remembers Your Location
Execution Policy is Per-User by Default
Related Topics
- Cmdlets Introduction - Understanding PowerShell commands
- Help System - Getting help and learning
- Variables & Data Types - Storing and using data
- Functions - Creating reusable scripts