Variables

Variables are important for the creation of good scripts. A variable is like a placeholder, and every kind of object and type can be stored in that variable. You can save data that is often used within a script in it or you can make calculations with it. You can forward variables containing data to functions, to make changes or output with them. Variables are the heart of every good script, cmdlet, function, and module. We will get into these topics in the next chapters. A good tip is to always learn the best practices for writing code as soon as possible. Therefore, I will introduce you to the best practices with each new topic. 

Some of the basics that you should be aware of are the four capitalization variations:

  • lowercase: All letters lowercase, no word separation
  • UPPERCASE: All letters capitals, no word separation
  • PascalCase: Capitalize the first letter of each word
  • camelCase: Capitalize the first letter of each word, except the first

Regarding variables, there are two capitalization rules that you need to know. The first is that, for global variables, where you should always use PascalCase, and the second one is that, for all other kinds of variables, where you should use camelCase. In addition, you should always use self-explanatory names for your variables, as in the following example:

#string
$varWithString = "Test"
$varWithString = 'Test'

#int
$varWithInt = 5

#get type
$varWithString.GetType()
$varWithInt.GetType()

#working with strings
$varCombined = $varWithString + $varWithInt
$varCombined #Test5

#calculation
$calculatedVar = $varWithInt + 5
$calculatedVar #10

Keep in mind that even you will have problems when taking a look at your, which that you implemented months ago. The better you write your code at first, the easier it will be to work with later on.


You can find great documentation of PowerShell's best practices at https://github.com/PoshCode/PowerShellPracticeAndStyle.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.239.148