-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Windows PowerShell for .NET Developers - Second Edition - Second Edition
By :
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.
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
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:
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:
Windows PowerShell in Search programs and files.Here, ISE is short for Integrated Scripting Environment.
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:
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:




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:

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:

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.
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 /?
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.
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.
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.
$MaximumHistoryCount is a built-in variable that returns the actual value of the command's history. This takes precedence over GUI.
The following image illustrates the Font tab:

The following image illustrates the Layout tab:

The following image illustrates the Colors tab:

Press F7 to see the history; this pops up a tiny window as shown in the following image:

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:

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:
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:

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:

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:
In comparison with the PowerShell console host, an ISE has a lot of benefits, and I will list a few of them here:
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:
The following image shows the Settings window of Script Browser:

Some benefits of Script Browser are as follows:
The link to download Script Browser is http://www.microsoft.com/en-us/download/details.aspx?id=42525.
Change the font size
Change margin width
Change background colour