Overview of Windows PowerShell 3.0

  • 2/15/2013
Microsoft Windows PowerShell 3.0 is a significant improvement for the Windows network administrator. This chapter, from Windows PowerShell 3.0 Step by Step, explains PowerShell basics and then goes on to install, run, and get help with PowerShell. It continues by showing how to run and get help with cmdlets and configure PowerShell to run scripts.

After completing this chapter, you will be able to:

  • Understand basic use and capabilities of Windows PowerShell.

  • Install Windows PowerShell.

  • Use basic command-line utilities inside Windows PowerShell.

  • Use Windows PowerShell help.

  • Run basic Windows PowerShell cmdlets.

  • Get help on basic Windows PowerShell cmdlets.

  • Configure Windows PowerShell to run scripts.

The release of Microsoft Windows PowerShell 3.0 marks a significant advance for the Windows network administrator. Combining the power of a full-fledged scripting language with access to command-line utilities, Windows Management Instrumentation (WMI), and even VBScript, Windows PowerShell provides the power and ease of use that have been missing from the Windows platform since the beginning of time. As part of the Microsoft Common Engineering Criteria, Windows PowerShell is quickly becoming the management solution for the Windows platform. IT professionals using the Windows Server 2012 core installation must come to grips with Windows PowerShell sooner rather than later.

Understanding Windows PowerShell

Perhaps the biggest obstacle for a Windows network administrator in migrating to Windows PowerShell 3.0 is understanding what PowerShell actually is. In some respects, it is a replacement for the venerable CMD (command) shell. In fact, on Windows Server 2012 running in core mode, it is possible to replace the CMD shell with Windows PowerShell so that when the server boots up, it uses Windows PowerShell as the interface. As shown here, after Windows PowerShell launches, you can use cd to change the working directory, and then use dir to produce a directory listing in exactly the same way you would perform these tasks from the CMD shell.

Windows PowerShell
Copyright (C) 2012 Microsoft Corporation. All rights reserved.

PS C:\Users\administrator> cd c:PS C:\> dir


    Directory: C:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         3/22/2012   4:03 AM            PerfLogs
d-r--         3/22/2012   4:24 AM            Program Files
d-r--         3/23/2012   6:02 PM            Users
d----         3/23/2012   4:59 PM            Windows
-a---         3/22/2012   4:33 AM         24 autoexec.bat
-a---         3/22/2012   4:33 AM         10 config.sys


PS C:\>

You can also combine traditional CMD interpreter commands with some of the newer utilities, such as fsutil. This is shown here:

PS C:\> md c:\test


    Directory: C:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         4/22/2012   5:01 PM            test


PS C:\> fsutil file createnew C:\test\mynewfile.txt 1000
File C:\test\mynewfile.txt is created
PS C:\> cd c:\test
PS C:\test> dir


    Directory: C:\test


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         4/22/2012   5:01 PM       1000 mynewfile.txt


PS C:\test>

The preceding two examples show Windows PowerShell being used in an interactive manner. Interactivity is one of the primary features of Windows PowerShell, and you can begin to use Windows PowerShell interactively by opening a Windows PowerShell prompt and typing commands. You can enter the commands one at a time, or you can group them together like a batch file. I will discuss this later because you will need more information to understand it.

Using cmdlets

In addition to using Windows console applications and built-in commands, you can also use the cmdlets (pronounced commandlets) that are built into Windows PowerShell. Cmdlets can be created by anyone. The Windows PowerShell team creates the core cmdlets, but many other teams at Microsoft were involved in creating the hundreds of cmdlets shipping with Windows 8. They are like executable programs, but they take advantage of the facilities built into Windows PowerShell, and therefore are easy to write. They are not scripts, which are uncompiled code, because they are built using the services of a special .NET Framework namespace. Windows PowerShell 3.0 comes with about 1,000 cmdlets on Windows 8, and as additional features and roles are added, so are additional cmdlets. These cmdlets are designed to assist the network administrator or consultant to leverage the power of Windows PowerShell without having to learn a scripting language. One of the strengths of Windows PowerShell is that cmdlets use a standard naming convention that follows a verb-noun pattern, such as Get-Help, Get-EventLog, or Get-Process. The cmdlets using the get verb display information about the item on the right side of the dash. The cmdlets that use the set verb modify or set information about the item on the right side of the dash. An example of a cmdlet that uses the set verb is Set-Service, which can be used to change the start mode of a service. All cmdlets use one of the standard verbs. To find all of the standard verbs, you can use the Get-Verb cmdlet. In Windows PowerShell 3.0, there are nearly 100 approved verbs.

Installing Windows PowerShell

Windows PowerShell 3.0 comes with Windows 8 Client and Windows Server 2012. You can download the Windows Management Framework 3.0 package containing updated versions of Windows Remote Management (WinRM), WMI, and Windows PowerShell 3.0 from the Microsoft Download center. Because Windows 8 and Windows Server 2012 come with Windows PowerShell 3.0, there is no Windows Management Framework 3.0 package available for download—it is not needed. In order to install Windows Management Framework 3.0 on Windows 7, Windows Server 2008 R2, and Windows Server 2008, they all must be running at least Service Pack (SP) 1 and the Microsoft .NET Framework 4.0. There is no package for Windows Vista, Windows Server 2003, or earlier versions of the operating system. You can run both Windows PowerShell 3.0 and Windows PowerShell 2.0 on the same system, but this requires both the .NET Framework 3.5 and 4.0.

To prevent frustration during the installation, it makes sense to use a script that checks for the operating system, service pack level, and .NET Framework 4.0. A sample script that will check for the prerequisites is Get-PowerShellRequirements.ps1, which follows.

Get-PowerShellRequirements.ps1
Param([string[]]$computer = @($env:computername, "LocalHost"))
 foreach ($c in $computer)
  {
    $o = Get-WmiObject win32_operatingsystem -cn $c
    switch ($o.version)
    {
        {$o.version -gt 6.2} {"$c is Windows 8 or greater"; break}
        {$o.version -gt 6.1}
          {
           If($o.ServicePackMajorVersion -gt 0){$sp = $true}
           If(Get-WmiObject Win32_Product -cn $c |
              where { $_.name -match '.NET Framework 4'}) {$net = $true }
           If($sp -AND $net) { "$c meets the requirements for PowerShell 3" ; break}
           ElseIF (!$sp) {"$c needs a service pack"; break}
           ELSEIF (!$net) {"$c needs a .NET Framework upgrade"} ; break}
        {$o.version -lt 6.1} {"$c does not meet standards for PowerShell 3.0"; break}
        Default {"Unable to tell if $c meets the standards for PowerShell 3.0"}
    }

  }

Deploying Windows PowerShell to down-level operating systems

After Windows PowerShell is downloaded from http://www.microsoft.com/downloads, you can deploy it to your enterprise by using any of the standard methods. Here are few of the methods that you can use to accomplish Windows PowerShell deployment:

  • Create a Microsoft Systems Center Configuration Manager package and advertise it to the appropriate organizational unit (OU) or collection.

  • Create a Group Policy Object (GPO) in Active Directory Domain Services (AD DS) and link it to the appropriate OU.

  • Approve the update in Software Update Services (SUS) when available.

  • Add the Windows Management Framework 3.0 packages to a central file share or webpage for self service.

If you are not deploying to an entire enterprise, perhaps the easiest way to install Windows PowerShell is to download the package and step through the wizard.