Tag Archives: PowerShellGet

Learning PowerShell

Every few weeks or so, over the last 10 years or so, there is a new post somewhere, where somebody is asking about the best way to learn PowerShell. It has probably been a thing beginning in 2006. While I have not been evaluating this since then, I have noticed it long enough to feel confident in this statement. So confident that I have heard it for a long time, that I will remind everyone that I even wrote about it in 2016. In the time since the initial PowerShell launch, books have been written, videos have been recorded, new domains established, and web hosting purchased too. All of these have been done in the name of teaching other people PowerShell.

For all the posts I see where someone is looking for the best way to learn, I cannot recall hearing anyone mention the official PowerShell documentation. Okay, I saw a link somewhere recently, but it is not often mentioned. I have been spending some time there recently and it is undeniably thorough. It is well-written, explained clearly, and written for the newest of PowerShell users. The examples start with only the basic structures required. Not everyone out that does that, and it can be frustrating at times. I have been there, where I only want the simplest of examples from which to work. But some of the content out there is wildly complex and is obviously intended for an advanced audience. While I have always tried to write intermediate content with basic examples, not everyone has and that is fine.

Here is a link to the PowerShell About topics. Maybe spend a moment there right now. I dare you to challenge yourself to learn one new thing before you continue reading this post. On the linked page, scroll down until you find a topic that is not that familiar to you and read it — maybe even just a paragraph or two is all you will need. Stop when you learn something new. I will do the same and share what I learned…

For me, I scrolled down to about_Debuggers, where I learned that the Get-PSCallStack command is considered one of the Debugger Cmdlets. I know the command and have even written about it myself, but I never considered that it would be a debugging tool, although it makes perfect sense that it would. There is even documentation on all the, Microsoft-written commands. Here is where you can read more about Get-PSCallStack.

Here is my post on that command — I do not even mention debugging, so yes, I learned something new. It took less than five minutes. Hopefully, you really played along and learned something too. You are welcome to Tweet me what you learned and that way we may all learn something from it. Be careful though, learning and teaching people about PowerShell can be addicting.

Another challenge is that if you think you know a PowerShell topic, take a minute and read top to bottom on one of the About pages. You are probably going to learn something new. I was reading about the -replace operator recently and learned a few things I had never considered or known, simply because I have never needed to use it that way. Good to have read before, as someday I may be out there searching for a solution.

If you want to learn PowerShell, do not forget about the product’s own documentation; it has been written for you, by the people intimately involved in PowerShell. Like, it is their job. They create the resources that you can use to completely learn PowerShell — and it is all free. Maybe read two a day. Your job may provide some room for personal development, so read one to start the day and one to end it.

Changing direction a bit, the next thing I want to discuss is a script I wrote years ago. It is available on the PowerShell Gallery and it is called the Start-1to100Game3.0. If you are wondering, yes, it still pains me that I included the version of the script in the script’s name. Let that be a lesson.

This project was awesome. PowerShell chooses a number and then you select higher or lower until you guess the number. Simple concept, but well written. It kept totals, allowed continuous gameplay. I wrote about it many years ago, so read about it here if you want to know more.

You can download the poorly-named script using the below command.

Install-Script -Name Start-1to100Game3.0

By default, the script is going to be downloaded to the Scripts directory. On my computer, that is C:\Users\tommymaynard\Documents\PowerShell\Scripts. As you may know, you will have to dot source the script (as it is written as a function). This will allow you to run the function in the current scope. There is more information about this in about_Scripts. Here is the information you might be after on that page:

The dot sourcing feature lets you run a script in the current scope instead of in the script scope. When you run a script that is dot sourced, the commands in the script run as though you had typed them at the command prompt. The functions, variables, aliases, and drives that the script creates are created in the scope in which you are working. After the script runs, you can use the created items and access their values in your session.”

I could not have said it any better myself. So, keep in mind the About pages for learning PowerShell and enjoy a quick game of the 1 to 100 game — why not?

But, before we wrap up, what do these two things have in common!? Why have I placed these two topics — learning PowerShell using the About topics and my 1 to 100 script in the same post? Hopefully, I will be telling you soon. I am doing that thing where I am writing posts here before I write the PowerShell to back them up. So…, as long as I complete that project, some of this may actually make sense. And if it does not, at least we all got a reminder about the PowerShell About Topics and learned about an interactive game you, or your little one(s) — if you have those — can play. At this point, perhaps I will be back with more!

The 1 to 100 Game, Updated

Notes: Download link at the bottom of this post.

My just turned, five-year-old daughter was introduced to my PowerShell 1 to 100 game around two years ago. As I watched her play, I always thought I should make some changes to the game. Well today, I have a newer version, that I’d like to briefly share, and discuss.

First off, you may need to know how to play the 1 to 100 game. It was common in my household as a child, but it’s possible it wasn’t in everyone’s. It works this way: The computer — the function, more or less — chooses a random number between 1 and 100 and you have to try and guess the number. If you choose a number lower than the computer’s number, it will tell you to choose a higher number. If you choose a number higher than the computer’s number, it will tell you to choose a lower number. You continue to do this until you guess the correct number.

What I always thought was necessary for this game was to make it possible to choose whether you even want to use 1 and 100. This 3.0 version of the function will allow you to choose the minimum and maximum numbers. That way, younger kids can play 1 to 10, or 1 to 20. Maybe your kiddo is learning their fifties: You could even play 50 to 59 if you desired. Maybe you have a little downtime while you’re waiting for a server restart; you can play it too. There’s some value here, but not perhaps the value you might typically expect with a PowerShell function. It does have an example of thrice nested Do-While loops. That’s how I pushed the previous version. Skip downloading this 2.0 version, and use the bottom link for version 3.0.

In the older version, it showed the aggregate totals by default. Now, you have to include the ShowTotals switch parameter, if you want to see that collected information. You probably do, if you’re going to play more than once or twice in a row. This information includes nuggets of information like how many games you’ve played, your total number of attempts to guess the right number across all games, and your average attempts per game. These totals have drastically improved, too. In fact, it’ll continue to show the previous totals, and the new totals, after each game. If this is confusing, then take a look at gameplay images further below, and try it out for yourself.

This first image is a game from 1 to 10 that doesn’t include the ShowTotals parameter.

This next image is from two back-to-back games from 1 to 3, that do include the ShowTotals parameter.

So it’s been said, the function defaults to 1 and 100 if you don’t supply minimum and maximum numbers. Try it out when you have a minute, and definitely show it to your kiddos, too. If my daughter is any indication, your kids will enjoy it, as well. I should mention, that she was pestering me to update this function after I first mentioned wanting to make some changes for her. A four-year-old pestering me to write PowerShell: I loved it.

On a final note, there is some redundant code in the function. Since this is just a game, and I have bigger projects, I’m leaving it as-is for now. Maybe I’ll come back around some time and clean it up… we’ll see.

You can download it from the PowerShell Gallery, or better yet, from your PowerShell host application using PowerShellGet and the Install-Script function: Install-Script -Name Start-1to100Game3.0.