Datasheet
UNDERSTAND VARIABLES AND TYPES 21
Getting Started with
Windows PowerShell
Basics
PART 1
Understand Variables and Types
Now that you know the core components involved, you can begin to use Windows
PowerShell to perform some tasks. e following sections will review the core
Windows PowerShell elements that every SharePoint administrator must know.
ese include declaring and using variables and working with various data types,
including the core value types as well as arrays and hash tables.
Declare and Use Variables
In all programming and scripting languages, storage locations are needed to hold
data so that it can be accessed at a later time. ese locations are referred to as
variables.
Windows PowerShell variables are designated with a dollar sign. For example,
declaring a variable that stores the results of the
Get-SPFarm
cmdlet would look
like this:
PS C:\> $farm = Get-SPFarm
e actual variable is
farm
, the
$
in front of it simply lets you know that it is a
variable. e variable
$farm
stores an instance of the
Microsoft.SharePoint
.Administration.SPFarm
class. is is great because you don’t have to think
about what type a cmdlet returns. ( is is due to the adaptive type system, which is
explained later in this chapter.)
TIP
Windows PowerShell supports the ability to require that all variables be explicitly
declared in a manner similar to the one used for the “Option Explicit” feature in Visual
Basic. An exception would be thrown if a variable is referenced before a value is assigned.
To enable this feature, type
set-psdebug -strict
, and to disable it, type
set-
psdebug -off
.
e name of variables can contain letters, numbers, spaces, or special characters.
ere are several accepted naming conventions that you may adopt. While vari-
ables may contain spaces, the use of spaces would require the variable name to be
enclosed in curly braces:
PS C:\> ${SharePoint Site Name} = “Home”
PS C:\> ${Content Database} = “WSS_Content”
c01.indd 21c01.indd 21 5/16/2011 11:12:32 AM5/16/2011 11:12:32 AM