Introduction to PowerShell for beginners on Windows
When just clicking with a mouse isn't enough anymore and you need to automate.
Introduction to PowerShell for beginners on Windows
When I started working on the Service Desk, I did everything by “clicking around.” I needed to find the serial number (Service Tag) of a remote computer? I found it in some asset management tool. I wanted to see if a user has a locked account in Active Directory? I opened the graphical Active Directory Users and Computers (ADUC) console.
But then I found out that for the same operations, I only need one command in PowerShell entered in 3 seconds, I save 2 minutes of loading a clunky graphical program, and as a bonus, I can do the same task on a hundred computers at once.
From command line to objects
The difference between the old classic command line (cmd.exe) and PowerShell is diametrical.
The old cmd or bash in Linux take everything as text (string). You search for information in text.
PowerShell perceives everything as objects. Every running process is not just a line of letters; it is a full-fledged virtual “package” of information where you can ask, “How much RAM are you taking up?” or “What is your launcher ID?”.
What is a Pipeline?
The magic of PowerShell lies in the | character (the pipe, on the keyboard using Shift + \ or AltGr + W). The pipeline takes a whole box of information from the first command and forwards it to the command on the other side.
graph LR
A[Get-Process<br/>Get all running programs] -->|Sends objects| B[Where-Object<br/>Filter those taking too much memory]
B -->|Sends filtered| C[Stop-Process<br/>Kill those programs!]
style A fill:#a2d2ff,stroke:#333
style B fill:#ffd6a5,stroke:#333
style C fill:#ffadad,stroke:#333
If we were to translate this diagram into real code, it would be one line:
Get-Process | Where-Object {$_.WorkingSet -gt 500MB} | Stop-Process *(Warning: Do not run this on your production machine unless you like surprises).*
The Verb-Noun rule
PowerShell uses perfect nomenclature. Almost all its functions consist of an action (Verb) and a subject (Noun).
- Do you want to read an item?
Get-Item - Do you want to park a service?
Stop-Service - Do you want to create a new file?
New-Item
You don’t have to remember weird Linux abbreviations like ls, grep, or awk. Just use your head and the Get-Help command, which acts as an offline encyclopedia of all PowerShell capabilities.
Starting to write simple scripts requires breaking an initial mental barrier, but once you break through it, you never want to go back to clicking.