Home > Sample chapters

Overview of Windows PowerShell 3.0

Working with Windows PowerShell

This section will go into detail about how to access Windows PowerShell and configure the Windows PowerShell console.

Accessing Windows PowerShell

After Windows PowerShell is installed on a down-level system, it becomes available for immediate use. However, using the Windows flag key on the keyboard and pressing R to bring up a run command prompt—or mousing around and choosing Start | Run | Windows PowerShell all the time—will become time-consuming and tedious. (This is not quite as big a problem on Windows 8, where you can just type PowerShell on the Start screen). On Windows 8, I pin both Windows PowerShell and the PowerShell ISE to both the Start screen and the taskbar. On Windows Server 2012 in core mode, I replace the CMD prompt with the Windows PowerShell console. For me and the way I work, this is ideal, so I wrote a script to do it. This script can be called through a log-on script to automatically deploy the shortcut on the desktop. On Windows 8, the script adds both the Windows PowerShell ISE and the Windows PowerShell console to both the Start screen and the taskbar. On Windows 7, it adds both to the taskbar and to the Start menu. The script only works for U.S. English–language operating systems. To make it work in other languages, change the value of $pinToStart or $pinToTaskBar to the equivalent values in the target language.

The script is called PinToStartAndTaskBar.ps1, and is as follows:

PinToStartAndTaskBar.ps1

$pinToStart = "Pin to Start"
$pinToTaskBar = "Pin to Taskbar"
$file = @((Join-Path -Path $PSHOME  -childpath "PowerShell.exe"),
          (Join-Path -Path $PSHOME  -childpath "powershell_ise.exe") )
Foreach($f in $file)
 {$path = Split-Path $f
  $shell=New-Object -com "Shell.Application"
  $folder=$shell.Namespace($path)
  $item = $folder.parsename((Split-Path $f -leaf))
  $verbs = $item.verbs()
  foreach($v in $verbs)
    {if($v.Name.Replace("&","") -match $pinToStart){$v.DoIt()}}
  foreach($v in $verbs)
    {if($v.Name.Replace("&","") -match $pinToTaskBar){$v.DoIt()}} }

Configuring the Windows PowerShell console

Many items can be configured for Windows PowerShell. These items can be stored in a Psconsole file. To export the console configuration file, use the Export-Console cmdlet, as shown here:

PS C:\> Export-Console myconsole

The Psconsole file is saved in the current directory by default and has an extension of .psc1. The Psconsole file is saved in XML format. A generic console file is shown here:

<?xml version="1.0" encoding="utf-8"?>
<PSConsoleFile ConsoleSchemaVersion="1.0">
  <PSVersion>3.0</PSVersion>
  <PSSnapIns />
</PSConsoleFile>

Controlling PowerShell launch options

  1. Launch Windows PowerShell without the banner by using the -nologo argument. This is shown here:

    PowerShell -nologo
  2. Launch a specific version of Windows PowerShell by using the -version argument. (To launch Windows PowerShell 2.0, you must install the .NET Framework 3.5). This is shown here:

    PowerShell -version 2
  3. Launch Windows PowerShell using a specific configuration file by specifying the -psconsolefile argument, as follows:

    PowerShell -psconsolefile myconsole.psc1
  4. Launch Windows PowerShell, execute a specific command, and then exit by using the -command argument. The command itself must be prefixed by an ampersand (&) and enclosed in curly brackets. This is shown here:

    Powershell -command "& {Get-Process}"