Home > Sample chapters

Overview of Windows PowerShell 5.0

Working with Windows PowerShell

This section goes 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, pressing the Windows logo key on the keyboard and pressing R to bring up a run dialog box—or using the mouse to choose Start | Run | PowerShell all the time—will become time-consuming and tedious. (This is not quite as big a problem on Windows 10, where you can just enter PowerShell on the Start screen.) On Windows 10, I pin both Windows PowerShell and the Windows PowerShell ISE to both the Start screen and the taskbar. On Windows Server 2012 R2 running Server Core, 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 10, 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 US English–language operating systems. To make it work in other languages, change the value of $pinToStart and $pinToTaskBar to the equivalent values in the target language.

The script is called PinToStart.ps1, and is as follows.

PinToStart.ps1

$pinToStart = "Pin to Start"

$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()}}

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>5.0.10224.0</PSVersion>
  <PSSnapIns />
</PSConsoleFile>

Controlling Windows 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. This is shown here.

    PowerShell -version 3
  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 braces. This is shown here.

    Powershell -command "& {Get-Process}"