Tag Archives: Microsoft Virtual Academy

How Do I Start to Learn PowerShell?

I’ve been around the Windows PowerShell community awhile, and its various forums, and have noticed a consistent theme: People often ask how and what to use to learn PowerShell. There’s plenty of articles and other content out there — ten years’ worth now — and people still ask. While I began this site to help teach PowerShell, depending on the day, it may not always the best place to start. So, what is?

There’s two things I’ve often recommended to help people learn PowerShell. One is what I would consider to be the de facto standard of all introductory PowerShell reading, Learn Windows PowerShell in a Month of Lunches, Second Edition written by Don Jones and Jeffery Hicks and published by Manning Publications.

I read this book after I had already spent a couple years learning PowerShell. While I appreciated seeing what I missed in my own education, I realized with every page turn that I could’ve learned everything I knew, in a lot less time. All I would’ve had to do was buy the book first, and make a commitment to read it. If you’re new to PowerShell and you suspect that you’re going to be a Windows system administrator in the future, then buy it and read it now, before you end up hating yourself for not doing it sooner. Let me know if I get the cue to tell you, “I told you so,” or not. I hope I don’t.

The other thing I’d recommend are two video series found on Microsoft Virtual Academy. This really helped solidified some concepts. The first one is called Getting Started with PowerShell 3.0 Jump Start. The second series, to be watched after the first, is called Advanced Tools & Scripting with PowerShell. 3.0 Jump Start. While these both focus on PowerShell 3.0, they are both still quite relevant to the current release (PowerShell 5.0, at the time of this writing). I’m not sure which I’d recommend you do first — the book or the videos — as I’m not sure if one sequence would be better than the other. Either way, do both as they ensure a good amount of beneficial exposure.

There you go. These are my two, top recommendations for learning Windows PowerShell. It should be said, that in addition to these two, one of the things I did (and I’ve said it several times now), is ensured I learned at least one new thing about PowerShell every day (no matter how big or small, or what day it was). PowerShell is an important part of the future as a Windows system administrator, whether or not, you believe that right now.

Using OutVariable — Why Don’t I Do that More Often?

This week, Microsoft Virtual Academy had two live events about DSC (Desired State Configuration), hosted by Jeffery Snover and Jason Helmick. I watched as much as I was able, but there were some problems at work that demanded my attention, and so I was grudgingly pulled away from a good portion of both sessions. Luckily for me, and for you, if you missed them, is that the videos should be up in the next two to three weeks. That will allow anyone who is interested the ability to move through the modules (think sections, not PowerShell modules) around other ongoing tasks — like work.

I didn’t start this post to discuss DSC, but instead because of what I watched Jeffery Snover do several times. While I’ve always been aware of the existence of the -OutVariable common parameter, I’m not even sure if I’ve ever used it or not (although I’m certain I’ve used -ErrorVariable). This parameter is a great way to view your command’s results immediately, and write them to a variable at the same time.

In this example, we return the computers’ names from (all of) Active Directory (AD) that have the word ‘physical’ somewhere inside their description property. The problem here is that if we need to generate this list a second time, we’ll have to run the command again. This can be resource intensive, depending on the command, and not inline with best practice — at least, my best practice.

PS C:\> (Get-ADComputer -Filter {Description -like '*physical*'}).Name
DC01
DC02
DC03
WEB01
WEB02

In this example, we write our results to the variable $Physical. The difference here is that we don’t write the results to the screen automatically, but only when we echo the variable’s contents.

PS C:\> $Physical = (Get-ADComputer -Filter {Description -like '*physical*'}).Name
PS C:\> $Physical
DC01
DC02
DC03
WEB01
WEB02

In this example, we combine the best of both worlds: instant results written to the screen, with the “same” values stored in a variable. Notice that when I echo the variable, $P, it returns more than just the Name. This is because all the properties were written to the variable, before we displayed only the Name property. Note: I’ve concatenated the results after the first computer’s full results.

PS C:\> (Get-ADComputer -Filter {Description -like '*physical*'} -OutVariable P).Name
DC01
DC02
DC03
WEB01
WEB02
PS C:\> $P
DistinguishedName : CN=DC01,OU=Domain Controllers,DC=mydomain,DC=com
DNSHostName       : dc01.mydomain.com
Enabled           : True
Name              : DC01
ObjectClass       : computer
ObjectGUID        : ...
SamAccountName    : DC01$
SID               : ...
UserPrincipalName :
...

Here’s how we can return only the Name property, using this variable.

PS C:\> $P.Name
DC01
DC02
DC03
WEB01
WEB02

Hopefully I can remember to use this common parameter more often. We’ve been taught to store our results in a variable, so we aren’t continually performing resource intensive queries. This is a great way to do that, with the option to have the results of a command written to the screen immediately. Adios, friends.