Scripting with PowerShell
PowerShell is a powerful task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. It's designed for system administrators and power users, enabling them to control and automate administration of Windows and other platforms.
Why Use PowerShell for Scripting?
- Object-Oriented: Unlike traditional shells that deal with text streams, PowerShell processes objects, making data manipulation more structured and reliable.
- Extensibility: It can interact with WMI, .NET Framework, COM objects, REST APIs, and more.
- Consistency: Offers a consistent syntax and object model across various Windows components and services.
- Remote Management: Built-in remoting capabilities allow you to manage remote computers seamlessly.
Your First PowerShell Script
Let's create a simple script to get system information.
Example: System Information Script
Create a new file named GetSystemInfo.ps1
and paste the following code:
# GetSystemInfo.ps1
# This script retrieves basic system information.
Write-Host "--- System Information ---"
# Get computer name
$ComputerName = $env:COMPUTERNAME
Write-Host "Computer Name: $($ComputerName)"
# Get Operating System details
$OS = Get-CimInstance Win32_OperatingSystem
Write-Host "OS Name: $($OS.Caption)"
Write-Host "OS Version: $($OS.Version)"
Write-Host "Build Number: $($OS.BuildNumber)"
# Get processor information
$Processor = Get-CimInstance Win32_Processor | Select-Object -First 1
Write-Host "Processor: $($Processor.Name)"
Write-Host "Number of Cores: $($Processor.NumberOfCores)"
# Get total and free physical memory
$Memory = Get-CimInstance Win32_PhysicalMemoryArray | Select-Object -First 1
$TotalMemoryGB = [math]::Round($Memory.Capacity / 1GB, 2)
Write-Host "Total RAM: $($TotalMemoryGB) GB"
$FreeMemory = (Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory - (Get-Counter '\Memory\Available MBytes').CookedValue
$FreeMemoryGB = [math]::Round($FreeMemory / 1GB, 2)
Write-Host "Available RAM: $($FreeMemoryGB) GB"
Write-Host "------------------------"
Running the Script
Open PowerShell and navigate to the directory where you saved the file. Then, execute it:
PS C:\Path\To\Your\Scripts> .\GetSystemInfo.ps1
Note: You might need to adjust your PowerShell execution policy to run scripts. You can do this by running
Set-ExecutionPolicy RemoteSigned
as an administrator.
Key PowerShell Concepts
- Cmdlets: These are the core commands in PowerShell, typically named in a
Verb-Noun
format (e.g.,Get-Process
,Set-Location
). - Providers: Allow you to access different data stores (like the registry or file system) as if they were drives.
- Variables: Use the dollar sign (
$
) prefix (e.g.,$myVariable
). - Pipelines: Use the pipe symbol (
|
) to pass the output of one cmdlet as input to another.
Common Tasks
- Managing services:
Get-Service
,Start-Service
,Stop-Service
- Managing processes:
Get-Process
,Stop-Process
- Working with files and folders:
Get-ChildItem
,Copy-Item
,Remove-Item
- Interacting with the registry:
Get-ItemProperty
,Set-ItemProperty
PowerShell's capabilities extend far beyond these basics. Dive deeper into the official Microsoft documentation and community resources to unlock its full potential for managing your systems.