3. At the Windows PowerShell prompt, enter
powershell C:\helloworld.ps1
The string ???Hello World!??? should be displayed on the screen. Congratulations! You
have now created and executed your first, albeit mundane, Windows PowerShell script.
VARIABLES
The concept of variables exists in every scripting and programming language; they essentially
allow you to name placeholders for values that you will use within the script. In
Windows PowerShell, you can use any name as a variable, but it must start with the dollar
sign ($). You can use any combination of letters, numbers, and symbols. You can even
use a space in the variable name, provided that you enclose the entire variable name in
curly braces {}. The following are valid declarations of variables:
$MyName = "Steve"
$x = 5
${Variable with space} = "See the curly braces!"
444 Microsoft Windows Server 2008 Administration
As you can see, defining a variable and assigning it a value is a fairly straightforward
endeavor. In fact, if you??™ve written batch files, you can do all of the same things above except
for the last one, which is using spaces in the variable name, using the SET command.
Windows PowerShell also supports typecasted variables. This means that you can
tell PowerShell what kind of value the variable is going to store. This is generally considered
best practice since it prevents strange bugs from occurring if you write more
complicated scripts.
Pages:
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471