Introducing Windows PowerShell
Windows PowerShell is an object-based command-line interface with a very powerful scripting language built on .NET. Windows PowerShell is designed with access to several cmdlets, functions, filters, scripts, aliases, and executables.
From Windows Management Framework 4.0 onward, a new configuration management platform has been introduced in PowerShell 4.0, which is Windows PowerShell Desired State Configuration (DSC).
Windows PowerShell is very enhanced in the version 5.0 April 2015 Preview release. This is not a stable release, but we will explore the new features introduced in Windows PowerShell 5.0. The current stable version is Windows PowerShell 4.0.
IT professionals and developers use their own statements or definitions based on the way they use PowerShell. Mostly, they define PowerShell as a scripting language. Yeah! This is true but not completely because Windows PowerShell does much more. However, the official page of Windows PowerShell defines PowerShell as an automation platform and scripting language for Windows and Windows Server that allows you to simplify the management of your systems. Unlike the other text-based shells, PowerShell harnesses the power of the .NET Framework, providing us with rich objects and a massive set of built-in functionality to help you take control of your Windows environments. Refer to the following URL:
https://msdn.microsoft.com/en-us/powershell
You may wonder, why Windows PowerShell? Why not VBScript? This is a very common question that most administrators think. It's good to know both, but VBScript is fading, and compared to PowerShell, it is very limited. Windows PowerShell can access all .NET libraries, which helps us explore DLLs easily; on the other hand, VBScript has limitations. If you are a beginner Windows PowerShell will be a better choice because it is easier to understand and very powerful. The Windows PowerShell cmdlets are rich and very easy to understand. Using Windows PowerShell, we can avoid repeated steps, automate administration tasks, avoid GUI clicks, and do much more. This is not limited to IT Professionals; developers can also do much more by exploring Windows PowerShell, such as performing development tasks faster than before.
From Windows PowerShell 5.0 onward, we can develop classes, parse structured objects using the new cmdlets, and do the package management easily using the PackageManagement
module. This module is introduced in version 5.0 but was formerly known as the OneGet
module; many such modules are enhanced in the new version.
Note
But wait, this is not all; you can refer to the following link to get a list of the new features in Windows PowerShell 5.0:
https://technet.microsoft.com/en-us/library/hh857339.aspx#BKMK_new50
Installing Windows Management Framework 5.0
Before we explore the PowerShell console host and the PowerShell ISE, let's install Windows Management Framework 5.0 April 2015 Preview (Latest Build). Windows Management Framework 5.0 is now supported on Windows 7 and Windows Server 2008 R2 SP1—this will be covered in Chapter 3, Exploring Desired State Configuration in detail. Windows Management Framework is shipped out of the box in the Windows 10 operating system and Windows Server Technical Preview. So, there is no need for manual installation.
To start the demo, we need to perform the following functions in our machine environment:
- Set up Windows 2012 R2 Data Center
- Download .NET Framework 4.5 (Included in Windows Server 2012)
- Download Windows Management Framework 4.0
- Download Windows Management Framework 5.0
Note
Windows 2008 R2 has PowerShell 2.0 by default. To install Windows Management Framework 5.0, we need to upgrade from Windows Management Framework 4.0.
To open Windows PowerShell, perform the following functions:
- Click on the start icon.
- Type
Windows PowerShell
in Search programs and files. - It lists Windows PowerShell ISE and Windows PowerShell.
Here, ISE is short for Integrated Scripting Environment.
Note
Ignore x86—it's a 32-bit Windows PowerShell application (we will not use it in any of our examples in this book).
It's best practice to know your PowerShell version before you start doing an exercise or start using scripts from the Internet.
To check the current version of PowerShell, you can simply run the following code:
$PSVersionTable
The following screenshot illustrates the output:
Windows Server 2012 R2 has WMF Version 4.0 by default. Let's upgrade it to WMF 5.0. If you are performing this task on Windows 7 or Windows 2008 R2 with SP1, install WMF 4.0 prior to WMF 5.0.
Following are the links where you can download the .NET framework and WMF from:
Note
Do read the system requirements before proceeding into the production environment. The installation of WMF 5.0 requires a reboot.
Once the .NET framework 4.0 is in place, update it to the .NET framework 4.5.
As per the Microsoft Document, the .NET framework 4.5 is included in Windows 8 and Windows Server 2012. We can skip the .NET framework 4.5 installation.
We need to identify and install the correct package of WMF 5.0. For Windows Server 2012, we need to download the 64-bit version of it, which is WindowsBlue-KB3055381-x64.msu
.
Open the MSU package and you will see the prompt, as shown in the following screenshot:
- Click on Yes.
- Read and accept the license agreement, as shown in the following screenshot:
- After this, the installation begins, as shown in the following screenshot:
- Click on Restart Now when you see the following window on your screen:
We have successfully installed WMF 5.0. As an outcome of this upgrade, now we also have the updated versions of Windows PowerShell, Windows PowerShell Desired State Configuration (DSC), and the Windows PowerShell ISE. Package manager and network switches cmdlets are included with this version.
The following image illustrates the Windows PowerShell 5.0 ISE:
The Windows PowerShell consoles
Windows PowerShell has two different consoles: the console host and the Windows PowerShell ISE host, which is GUI. To verify this, click on the start icon and search for PowerShell
on Windows Server 2012.
The following image illustrates the results of PowerShell:
Note
Ignore x86 in the Windows PowerShell ISE (x86) for now. This is a 32-bit ISE, and throughout this book, we will focus only on the 64–bit ISE.
The location of the Windows PowerShell console host and ISE is:
C:\Windows\System32\WindowsPowerShell\v1.0
The file name powershell.exe
is the console host and powershell_ise.exe
is the Integrated Scripting Environment.
The Windows PowerShell console host
This is where we begin with the PowerShell cmdlets. It's easy for on-the-fly cmdlet executions. The following image illustrates the PowerShell 5.0 console host:
By default, the background color is blue, and in the foreground, the color of the text is white.
There is no need to type out the complete command; the PowerShell console allows Tab Completion. For example, Get-Se
+ Tab completes the Get-Service
command. Tab completion is not only for the commands and parameters; we can also select the properties using tab key.
To execute the following PowerShell code:
Get-Service -Name BITS | Select -Property Name , Status
Windows PowerShell allows tab completion like this:
Get-Ser
+ Tab -N
+ Tab BITS | sel
+ Tab -Pro
+ Tab N
+ Tab
, S
+ Tab
There are multiple ways to start a PowerShell console: we could either use the Run dialog box, or type PowerShell
in an open command-line window. These techniques allow you to pass arguments to Windows PowerShell, including the switches that control how Windows PowerShell works and the parameters that execute additional commands. For example, you can start Windows PowerShell in the no-logo mode (which means that the logo banner is turned off) using the startup command, PowerShell -nologo
. By default, when you start Windows PowerShell via the command shell, Windows PowerShell runs and then exits. If you want Windows PowerShell to execute a command and not terminate, type PowerShell /noexit
, followed by the command text.
We can use this to schedule our script in task schedulers. To see all the switches, run the following command:
PowerShell.exe /?
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Setting up the console host using GUI
We can change the look and feel of the console host using GUI. To do this, we need to perform the following steps:
Click on the Windows PowerShell icon, which appears in the upper-left corner and select Properties. This has four tabs—Options, Font, Layout, and Colors. Let's explore each tab.
- Options: Using the Options tab, we can see Cursor Size, Command History, and Edit Options, and adjust them as we require.
While setting the cursor size, we have three options: Small, Medium, and Large. Here, small is 25 pixels, medium is 50 pixels, and large is 100 pixels.
The following image illustrates the Options tab:
By default, the command history retains the last 50 commands in a buffer. We can customize this by increasing or decreasing the Buffer Size value.
Tip
$MaximumHistoryCount
is a built-in variable that returns the actual value of the command's history. This takes precedence over GUI. - Font: Using the Font tab, we can change the size, style, and type. Before applying changes, we can preview the console host using the preview window.
The following image illustrates the Font tab:
- Layout: Using the Layout tab, we can set the screen buffer size, window size, and window position. Similarly to the Font tab, this also has preview window.
The following image illustrates the Layout tab:
- Colors: Using the Colors tab, we can set the screen text, screen background, pop up text, and popup background values. The current RGB values are shown in the Selected Color Values section. The modifications can be previewed before applying.
The following image illustrates the Colors tab:
Tip
Press F7 to see the history; this pops up a tiny window as shown in the following image:
Setting up the console host using PowerShell
The steps we explored under Graphical User Interface (GUI) are limited; we can actually do more using PowerShell. Here comes the importance of objects. In this exercise, we will adjust our console settings. To do all this, we need to know is Get-Host
command-let.
The Get-Host
cmdlet is used to do more of the Windows PowerShell console setup. In this exercise let's change the title of the PowerShell console host.
The default console host title is shown in the following image:
Administrator: Windows PowerShell is the default title. Now let's change this to Windows PowerShell 5.0 April 2015 Preview
. Run the following commands:
(Get-Host).UI.RawUI (Get-Host).UI.RawUI | GM
The preceding code returns the current settings and its members.
The output is illustrated in the following image:
WindowTitle
is a property for which we can get and set a value and whose datatype is String
. We will discuss this in detail in the Understanding Objects section. Now, let's execute the following code to change the title of the console host:
(Get-Host).UI.RawUI.WindowTitle = "Windows PowerShell 5.0 April 2015 Preview"
The change appears as illustrated in the following image:
Using GUI, we can set the cursor size to small, medium, or large, which allows only 25, 50, or 100 respectively. But, PowerShell allows us to customize more by executing the following command:
(Get-Host).UI.RawUI.CursorSize
The previous command returns the current cursor size. Run the following command:
(Get-Host).UI.RawUI.CursorSize.GetType()
This command returns the type as shown in the following image:
Now, let's change the cursor size to 72
by executing the following command:
(Get-Host).UI.RawUI.CursorSize = 72
The output is illustrated in the following image:
Exploring the Windows PowerShell ISE host using GUI
The Windows PowerShell ISE host is enhanced and exciting in version 5.0. This version has lots of new features and is user friendly. The ISE helps us to write scripts faster than a console. We will explore the features of ISEs in Chapter 3, Exploring Desired State Configuration.
Opening the PowerShell ISE is similar to opening the console host. Click on the start icon, search for PowerShell
, and open PowerShell ISE. The executable file for the PowerShell ISE resides in the same location as console host:
%windir%\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe
The following image illustrates the PowerShell ISE:
The PowerShell ISE host is divided into the following sections:
- 1: Script pane
- 2: Command pane
- 3: Commands add-on
The script pane's visibility can be adjusted using the keyboard shortcuts, Ctrl + 1, Ctrl + 2, Ctrl + 3, which sets the script pane to top, right, and maximized, respectively. This makes it easy for us to view the command and result that we need.
The command pane will be hidden if we maximize the script pane.
Let us learn more about commands add-on pane:
- By default, all the available modules will be shown. Click on the drop-down list to view the modules.
- Click on Refresh if any new modules are loaded.
- Type the command name, and it will show the output as illustrated in the following image:
- Enter the command.
- Select the command.
- Click on the help icon, and this opens up the help in GUI.
- Enter the parameters as required. The * sign denotes mandatory.
- The common parameters are not always required, but we will use them in the next chapters.
- We can run the code, copy it, or click on Insert, which appears in the command pane. Ensure that you haven't maximized the Script Pane.
An ISE allows us to cut, copy, paste, start IntelliSense, and start the snippets. Similarly to other windows operations, Ctrl + X, Ctrl + C, and Ctrl + Y will cut, copy, and paste, respectively.
IntelliSense gives us a better discoverability of cmdlets. A bunch of related commands will be shown in the drop-down menu, so we can easily choose the cmdlet we need. The Syntax tooltip lists the parameters to be used with the command, as shown in the following image:
- As soon as we start typing, IntelliSense shows us the list of commands that begin with the text that is typed. We can scroll down and choose the command we need to execute.
- The syntax tooltip shows the syntax of the command that we choose.
To get help about the command, simply type the command, select it, and press F1.
The output is illustrated in the following image:
The points marked in the figure are explained in the following list:
- 1: Select the command.
- 2: Press F1, which brings up the help window.
- 3: Click on Settings.
- 4: Select the sections you need to know. For example, unselect Examples to hide the example section in the help window.
Benefits of an ISE
In comparison with the PowerShell console host, an ISE has a lot of benefits, and I will list a few of them here:
- It's easy to use
- It's very user friendly
- There are shortcut keys to manage the script
- Writing the DSC configuration code is easy
- The snippets make our job much easier
- There is a script browser
- We have the Command add-on
- It shows a squiggly line in case of errors
The PowerShell ISE script browser
PowerShell ISE has a script browser feature that allows us to search for the script in TechNet's script gallery. This is optional but worth using! This is because using Script Browser, we can download and use the scripts available in TechNet's script gallery.
The following image illustrates Script Browser:
Following are a few options in Script Browser:
- You can search for the script you need. Type the script's name in the textbox provided, right-click on the script, and select either Add to favorites or the Download option.
- Your favorite scripts appears in the Favorites tab.
- The scripts that you download will appear in the Downloads tab.
- You can set the language, download locations and network settings by clicking on the settings icon in the upper-right corner.
The following image shows the Settings window of Script Browser:
Some benefits of Script Browser are as follows:
- Script Browser allows us to filter the search based on technologies
- Script Browser allows us to sort the script by popularity, ratings, and so on
Note
The link to download Script Browser is http://www.microsoft.com/en-us/download/details.aspx?id=42525.