Powershell 4n00bs Sample
Powershell 4n00bs Sample
Powershell 4n00bs Sample
4N00bs
Don Jones
This book is for sale at
http://leanpub.com/powershell-4n00bs
2. A Note on Code . . . . . . . . . . . . . . . . . . . . . 3
3. What is PowerShell? . . . . . . . . . . . . . . . . . . 4
What Came Before . . . . . . . . . . . . . . . . . . . . 6
Introducing PowerShell . . . . . . . . . . . . . . . . . 10
OK - Why Should I Care? . . . . . . . . . . . . . . . . 14
4. Installing PowerShell . . . . . . . . . . . . . . . . . 17
Windows PowerShell . . . . . . . . . . . . . . . . . . 17
PowerShell Core . . . . . . . . . . . . . . . . . . . . . 17
VS Code . . . . . . . . . . . . . . . . . . . . . . . . . . 17
The Internet . . . . . . . . . . . . . . . . . . . . . . . . 20
PowerShell Gallery . . . . . . . . . . . . . . . . . . . . 21
Your Own Gallery . . . . . . . . . . . . . . . . . . . . 21
Seeing What Modules and Commands You Have . 21
What’s in a Module? . . . . . . . . . . . . . . . . . . . 21
Explore What’s Out There . . . . . . . . . . . . . . . 21
like a bit too much. By 50, it’s all fallen apart and the kids are
trying to eat each other, because they’ve already eaten the
teacher.
Scale is important in all kinds of industries. Take cars, for
example. Back in the day, before good old Henry Ford came
along. Cars were put together by hand. That didn’t scale well.
If it took 5 guys to build a car, and you wanted to build 10 cars
at once, you needed 50 guys. If you wanted 20 at once, you
needed 100 guys. Eventually, you ran outta guys. But Henry
built machines, so that a car could be built by 2 guys using
all the machines — and faster, too! And those same 2 guys
could build cars all day! Henry increased his scale through
automation.
GUIs are a bit like those 5 guys building 1 car. If it takes you
5 minutes to complete a task, then it’ll take you those same 5
minutes every time you need to perform that task. If you do
that task 70 times a day, that’s pretty much all you’re going to
be doing all day. If the business needs that task performed 150
times, you’re either going to be putting in a lot of overtime or
hiring a friend. And you’re both going to end up really bored,
doing that same task over and over and over and over and
over.
So PowerShell is the Henry Ford of computer administration.
Instead of clicking buttons and dragging things, you write
down what you want the computer to do. You write it down
in a kind of language that both you and the computer can
understand. Then, when you need the task performed, you
just basically point to those instructions, and the computer
does it. It does it faster than you, and more reliably, because
it never gets bored of doing the same thing over and over and
over and over.
What is PowerShell? 6
time you experienced it, and then you’re like, “um, no, I’ll stay
inside all summer, thanks.” But seriously, lots of folks working
on computers don’t even think these things are problems,
but I’ll try and make a case that they are, indeed, big bad
problems.
The pipe is the important part, here. It’s the idea that one
command’s output can be used by another command. String-
ing them all together lets us achieve some powerful results,
such as shutting down just the process named httpd, which is
what the above command-line does. The problem here is that
traditional commands just produce these enormous wedges
of text as output. For example, here’s a bit of what ps outputs
if you just run it by itself:
What is PowerShell? 9
Both of these are poor solutions, because they just add to the
amount of information everyone has to remember.
Introducing PowerShell
First, we should get some terminology right.
In 2006, Microsoft introduced Windows PowerShell 1.0. Through-
out the next decade, they released new versions of Windows
PowerShell: 2.0, 3.0, 4.0, 5.0, and 5.1. Good times!
But for version 6, Microsoft took a different tack. In 2017,
they released the first version of PowerShell Core, or just
“PowerShell,” with no “Windows” attached. It’s technically
version 6, but it’s the first version that runs on Windows,
Linux, and macOS. It’s a bit trimmed-down from its Windows
cousin. And, at the same time, Microsoft announced that
Windows PowerShell would basically stop at version 5.1.
Plain-old PowerShell (more properly, “PowerShell Core”) is
the go-forward product, and that’s what this book is about.
From its beginning, though, PowerShell was designed to
solve three problems, two of which are the ones I’ve already
introduced you to.
Solution 1: Consistency
PowerShell’s first step was to take all the weird command
names and ditch ‘em. Instead, PowerShell defines a naming
What is PowerShell? 11
Solution 3: Admin-Friendly
There’s another problem that isn’t specific to traditional CLIs.
See, the fact is that Windows, Linux, and other operating
systems already have all the tools you need to perform almost
any task you might need. But most of those “tools” are really
designed for software developers, and they’re built in a way
that is comfortable and familiar to software developers, but
often confusing and antagonistic to anyone else. For example,
Windows has the ability to terminate a running app. All you
have to do is run code like this:
What is PowerShell? 13
1 System.Diagonstics.Process p =
2 New System.Diagnostics.Process()
3
4 p.GetProcessById(1234)
5 // assumes you know that 1234 is the app you want
6
7 p.Kill()
Easy, right? Well, if you’re a developer, it’s very easy. All you
do is type in that code, compile the code into an executable,
and run the executable. Takes maybe 10 minutes.
PowerShell would just want you to do this:
1 Stop-Process MyApp
PowerShell performed the task just six times, you’d have paid
off your investment, and every time PowerShell performed
the task after that, you’d be growing the economy. Work would
be happening, essentially for free. That’s how Henry Ford did
it, and that’s why you should care about PowerShell. The
first line on your resume should be, “Dedicated automation
specialist who, in one year, saved former employer $100k [or
whatever] in labor costs.”
Keep a log of what you automate. Figure out how long the task
took to perform manually, and about how many times a year
it was performed. Track the time it took you to automate it.
Figure out the money you saved, and add that to your resume
statement.
That is why you should care about PowerShell.
4. Installing PowerShell
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Windows PowerShell
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
PowerShell Core
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
VS Code
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
5. Your First Console
Experience
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Launch PowerShell
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Run a Command
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Your First Console Experience 19
Exit
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Built-In Commands
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
The Internet
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Where Do Commands Come From? 21
PowerShell Gallery
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
What’s in a Module?
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Update Help
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
How Do I Use This Thing? 23
Online Help
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Formatting Rules
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Try These
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Six Pipelines
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Wrapping Up
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
10. Filtering and
Selecting
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Filtering
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Selecting Properties
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Filtering and Selecting 29
Advanced Selecting
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Wrapping Up
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
11. The Very Basics of
Scripting
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Wrapping Up
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
12. Going Remote
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Enabling Remoting
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Connectivity
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Remote Jobs
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
Wrapping Up
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
13. Understanding
Your OS
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.
14. The Chapter of
Gotchas
This content is not available in the sample book. The book can
be purchased on Leanpub at http://leanpub.com/powershell-
4n00bs.