Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Cheat Sheet

Windows PowerShell 2 For Dummies

With the Windows PowerShell 2 scripting language, you can automate your Windows operating system. During that process, you may need to deal with automatic variables, comparison operators, COM and .NET objects, and conditional statements. Windows PowerShell 2 For Dummies explains how to deal with each and clues you in on creating, running, and looping scripts — and much more.

Commonly Used Automatic Variables in Windows PowerShell 2

PowerShell 2 helps you automate functions within Windows. Its automation functions include automatic variables, variables the system defines automatically that are given values based on the condition or context at that particular point in time. Some of PowerShell 2's most common automatic variables are in the following table:

Variable Name Description
$_ Contains the current object in the pipeline object.
$Args Array of undeclared parameters or values passed to a function, script, or script block.
$False Contains the value FALSE.
$Home Full path to the user's home directory
$NULL Contains NULL or empty value.
$Profile Full path to the Windows PowerShell user profile for default shell.
$Pwd Full path to the current directory.
$True Contains the value TRUE.

Commonly Used Comparison Operators in Windows PowerShell 2

As you use PowerShell 2 to help automate your Windows operating system, you use comparison operators to compare one value with another. Some of PowerShell 2's most common comparison operators are listed in the following table:

Operator Description
-lt less than
-le Less than or equal to
-gt Greater than
-ge Greater than or equal to
-eq Equal to
-ne Not equal to
-like Like (uses wildcard for pattern matching)
-match A match using Regular Expressions
-contains Used to see if a collection or group of items contains a given item

Using Conditional Statements in Windows PowerShell 2

As you automate your Windows operating system with PowerShell 2, you use conditional statements to run different script blocks depending on a specific condition, usually using an if/else or switch statement. Following are examples of conditional statements:

$size = "M"
if ($size -eq "S") {
     Write-Host "Small"
} elseif ($size -eq "M") {
     Write-Host "Medium"
} else {
     Write-Host "Large"
}
$size = "M"
switch ($size)
{
   "S"   {Write-Host "Small"}
   "M"   {Write-Host "Medium"}
   default {Write-Host "Large"}
}

Performing Loops in Windows PowerShell 2

As you automate your Windows operating system with PowerShell 2, you can automate some of the script you need as well. Loops run the same script block multiple times — often on changing values. A few examples follow:

for ($i = 1; $i -le 5; $i++) { Write-Host $i }
foreach ($i in Get-Alias) { Write-Host $i.name }
$i = 1
while ($i -lt 7) {
     Write-Host $i++
}
$i = 1
do
{
     Write-Host $i++
} while ($i -lt 7)

Creating COM and .NET Objects with Windows PowerShell 2

PowerShell 2 enables you to automate your Windows operating system, which entails dealing with objects: COM objects are created using the New-Object method with the -comobject parameter, whereas .NET objects are created by enclosing the class name in square brackets. An example of each follows:

$objWord = New-Object -comobject "Word.Application"
$objWord.visible = $true
$objWord.Quit
$ips = [System.Net.Dns]::GetHostAddresses("www.dummies.com")
$ips | Select-Object IPAddressToString

How to Create and Run a PowerShell Script

As you automate your Windows operating system with PowerShell 2, it helps to know how to create scripts that you may be able to loop and use more than once. The steps to create a script follow:

  1. Create the script in a plain text editor such as Notepad and save with a .PS1 file extension (for example, myscript.ps1).

  2. Run the script by entering the full path to the script (c:\scripts\myscript.ps1), or if it’s in the current directory, prefix it with a period followed by a backslash (.\myscript.ps1).

  3. If the path to the script contains a space, enclose the full path in quotation marks and prefix the entire thing with an ampersand (for example: &"C:\my scripts\myscript.ps1").

Unless you’re running a signed script, make sure you to set the correct execution policy using Set-ExecutionPolicy. To allow local scripts to run unsigned, run:

Set-ExecutionPolicy RemoteSigned
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!