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.
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:
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:
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
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.
Windows PowerShellin Search programs and files.
It lists Windows PowerShell ISE and Windows PowerShell.
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:
The following screenshot illustrates the output:
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
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:
The file name
powershell.exe is the console host and
powershell_ise.exe is the Integrated Scripting Environment.
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.
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.
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:
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:
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 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 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:
The previous command returns the current cursor size. Run the following command:
This command returns the type as shown in the following image:
(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:
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.
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.
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:
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.
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
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:
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:
The link to download Script Browser is http://www.microsoft.com/en-us/download/details.aspx?id=42525.