PowerShell primers

PowerShell for beginners: Scripts and loops

PowerShell primers

Show More
1 2 3 4 5 6 Page 3
Page 3 of 6

Making scripts useful, phase 1: Variables

Now if you buy that the whole point of scripting is to do things over and over again in a consistent way, then you have to buy the idea that you want to do the same action to different things. But how do you change the things that are being acted upon? Variables are how. Variables are kind of like holders, and you can put values, words, numbers -- basically anything -- within them.

In PowerShell, variables always have a dollar sign ($) before them.

Let me declare -- in other words, set up for the first time -- a few variables for you right now:

$name = 'Jon'

$number = 12345

$location = 'Charlotte'

$listofnumbers = 6,7,8,9

$my_last_math_grade = 'D+'

All I have to do to declare those variables is add a dollar sign, then use whatever name for the variable I want -- no spaces are allowed in the name itself -- and then a space, an equals sign, another space and then whatever I want the value of the variable to be.

If I want to have a variable with text as its value, I need to add a single quote on either side of the text. (There are some exceptions to this, but again my goal here is to keep it simple so we’ll stick with this for now.) You can also just declare a variable without putting a value in it. This kind of "reserves" the name, which is probably more helpful when you're in the middle of developing than it is at any other time.

You know what else you can put in a variable? The output of a cmdlet, which is a cute moniker that refers to the simplest bit of .Net-based code you can execute that actually returns a result, either from the PowerShell prompt or from a script. For example, the Get-Process cmdlet lists all processes, while the Get-PSSnapin cmdlet shows all current PowerShell snap-ins that enable new functionality.

To find out the total number of cmdlets available on the system, we can use:

(get-command).count

And at least for the system on which I am writing today's piece, that returned a result of 1,338 cmdlets.

Let's declare a variable to store that count in. We’ll call it

$numberofcmdlets

And let's store in that variable the output of the (get-command).count entry.

$numberofcmdlets = (get-command).count

PowerShell will tell you the current value of any variable if you just type its name into the prompt, so let's see if that worked:

PowerShell for Beginners

Success! Now you can use that variable as part of something else. For a simple example, let's look at the Write-Host cmdlet, which simply writes text to the screen of the machine hosting the PowerShell session. Write-Host has a bunch of capabilities, but in its simplest form, you can just say:

Write-Host "Whatever text I want, as long as it is inside double quotes."

Indeed, you can cut and paste that line into a PowerShell window and it’ll come out exactly like it goes in.

But you can integrate variables with Write-Host. You just call them with the dollar sign notation and work them right into your text. For example, I can say:

Write-Host "There are $numberofcmdlets commands available for use on this system."

And what does PowerShell return to us?

PowerShell for Beginners

Let's put variables aside for now, and move on to the next element of scripting: Decision making and looping.

1 2 3 4 5 6 Page 3
Page 3 of 6
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon