Book Image

Instant Windows PowerShell Guide

By : Harshul Patel
Book Image

Instant Windows PowerShell Guide

By: Harshul Patel

Overview of this book

Windows PowerShell has become a booming scripting language over the last couple of years. It has extensive support with an ample number of vendor products, providing a standardized platform for automation and administration. It has massive support for all Microsoft products which creates a layer that can easily automate everything. In the latest version, the PowerShell team has introduced much more functionality with thousands of CMDLETs, part of various modules.This book is a quick reference guide to enable you to get the most out of the latest Windows PowerShell techniques. In this book, you will find new enhancements in the latest version of PowerShell with some helpful examples. This book enables you to quickly move from older versions of PowerShell to Version 3.0 and Version 4.0.This practical, example-oriented book helps you to overcome the difficulty of using and discovering CMDLETs by providing precise information about everything that has been newly introduced in the latest version of Windows PowerShell. It also focuses on the new configuration management system with the help of DSC as a new feature of Windows PowerShell v4.0.You will learn how to use the newly introduced CMDLETs and parameters to perform daily routine tasks. You will also learn how to administer the servers remotely and maintain persistent sessions to provide continuity. You will gain an insight into writing efficient scripts by using various parameters, snippets, and workflows to gain more productivity. You will also be introduced to various modules like CimCmdlets, PSScheduledJob, PSDesiredStateConfiguration, and so on in order to enhance your scripts with the latest instrumentation. Finally this book will make you aware of the capabilities of PowerShell v4.0 and how to fully leverage the functionality introduced in the new version.
Table of Contents (7 chapters)

Typing enhancements (Intermediate)

In the previous recipes, we have covered the basic changes that took place with the release of Windows PowerShell v3.0. Let's have a look at typing enhancements in the Version 3 console.

We have tab completion for CMDLETs in each version of Windows PowerShell, especially in Version 3.0 where we have tab completion for parameter values as well.

Getting ready

We have some simplified syntax introduced in the latest version of Windows PowerShell with respect to the Where-Object and ForEach-Object CMDLETs.

How to do it...

  1. In Version 2.0, the following command retrieves a list of running processes, which have a handles count greater than 1000 from the local machine:

    PS C :\> Get-Process | Where-Object {$_.Handles -gt 1000}
  2. In Version 3.0, the following command does the same operation as the previous command statement:

    PS C :\> Get-Process | Where-Object Handles -gt 1000

    Let's check use of ForEach-Object and Where-Object by using the following points:

    • The following command statement lists down only files and directory names from the C:\Scripts location:

      PS C :\> Get-ChildItem C:\Scripts | ForEach-Object Name
    • The following command retrieves the list of running services on the local computer which have the win keyword in their names:

      PS C :\> Get-Service | Where-Object {$PSItem.Status -eq "Running" -and $PSItem.Name -like "*win*"}

How it works...

If we compare the preceding two different version's outputs, it is evident that PowerShell v3.0 has simplified syntax. Moreover, we do not need to use curly braces anymore to run a command statement.

Also, it automatically gets the previous command pipeline output as input for the Where clause. We don't need to explicitly provide the parameter value with the $_ syntax.

Novice users would find the $_ syntax a bit strange; now, in PowerShell v3.0, we can use $PSItem instead of $_.


It is recommended to use full syntax with curly braces and $PSItem when we draft a script.

The same is the case with Where-Object; we don't need to use curly braces and the $_ syntax if we are dealing with ForEach-Object in PowerShell v3.0.