Chapter 5. When Dollars Turn into Variables

In This Chapter

  • Saying hello to variables

  • Understanding data types

  • Using constant and read-only variables

  • Finding out about automatic variables

  • Working with objects through variables

Variables are one of the key building blocks of any scripting and programming language. Without them, you wouldn't be able to store values, easily manipulate objects, or use Windows PowerShell for anything more than simple tasks.

For some reason, every time I think of variables, I'm taken back to the dark days of high school algebra. Don't get me wrong; I actually ended up doing quite well in algebra, but in the beginning I had an unusually hard time getting the idea that some letter such as x could represent some seemingly arbitrary value. Once I finally grasped this enlightening idea, the rest of my mathematical learning experience was smooth sailing. (Okay, I admit it; calculus did throw me a curve ball, but I got through it!)

Variables in Windows PowerShell are just like variables in other scripting and programming languages in that they are names of things that represent some value, just as the almighty x in algebra always represents something else. Without variables, it would be difficult to make any useful scripts because you wouldn't be able to store any state information or preserve any values. In short, you wouldn't have any ability to know what happened in any other part of the script. In this chapter, I discuss what variables are as well as how they're ...

Get Windows PowerShell™ 2 For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.