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)

Working with the Out-GridView CMDLET (Intermediate)

Here, we would be learning about the different parameters of the Out-GridView CMDLET.

Getting ready

The Out-GridView CMDLET gets the output in a graphical form. It leverages the .NET Framework to construct an output window, so the .NET Framework is mandatorily required to deal with the Out-GridView CMDLET.

How to do it...

Try executing the following lines of code.

  1. The following command statement generates the output window along with all the services that are running, and we can select multiple service instances to stop them from having further pipeline executions:

    PS C :\> Get-Service | Out-GridView –PassThru | Stop-Service
  2. The following command creates an output window popup with "Process List" as the header:

    PS C :\> Get-Process | Out-GridView -Title "Process List"

How it works...

A few more parameters are introduced with the popular Out-GridView CMDLET.

  • -OutputMode <OutputModeOption>: By default, the Out-GridView CMDLET doesn't generate output objects except for an interactive console. Using the OutputMode parameter, you can explicitly define that you need to generate a specific number of objects as the output. It has three acceptable values as follows:

    • None: This is the default option and it doesn't generate any objects.

    • Single: This option provides only one input object to the next relevant CMDLET and passes it into the pipeline.

    • Multiple: This option can generate multiple input objects that can be used by the next relevant CMDLET and pass them into the pipeline. This option behaves in a way that is identical to the PassThru parameter.

  • -PassThru [<SwitchParameter>]: This parameter acts like the OutputMode parameter with the Multiple option. It generates multiple input objects based on what the user selects in the interactive window. These objects would be passed to the subsequent CMDLET in the pipeline.

  • -Title <String>: By default, the Out-GridView CMDLET generates an interactive window with a complete command statement as the title of the window. We can set the title manually using the Title parameter along with the Out-GridView CMDLET.

  • -Wait [<SwitchParameter>]: By default, with the execution of the Out-GridView CMDLET, the console prompt returns immediately. You can explicitly prevent the command prompt to return immediately with the Wait parameter along with the Out-GridView CMDLET.

There's more…

There are a few more CMDLETs that can be useful to operate the data in an efficient way. Some are listed as follows:


The Export-Csv CMDLET exports the output data into a CSV file.

  • -Append [<SwitchParameter>]: By default, the Export-Csv CMDLET overwrites the output to the specified file if it is already available in the defined location. Using the Append parameter, you can restrict that behavior and allow the CSV file to append further with the output content.


This parameter adds custom methods and properties to an object.

  • -NotePropertyMembers <IDictionary>: With this parameter, we can explicitly provide the list of custom property names and values to be added in a hash-table format using the Add-Member CMDLET.

  • -NotePropertyName <String>: This parameter passes property names to the Add-Member CMDLET.

  • -NotePropertyValue <Object>: This parameter passes a value object to property names that are defined with the NotePropertyName parameter.


    It is recommended to use NodePropertyName and NodePropertyValue together to provide custom properties with the Add-Member CMDLET.

  • -TypeName <String>: This parameter provides the name of the type. The type can be a class from the system namespace, and using this, you can also provide a short name for the type, for example:

    PS  C:\>$P = Get-ProcessPS C:\>$Job = Add-Member –InputObject $P -NotePropertyName CurrentStatus -NotePropertyValue Completed
    PS  C:\>$Job = Add-Member CurrentStatus Completed

    These command statements add the CurrentStatus property along with the value Completed to the $Job variable object.

    PS C:\>$Job.CurrentStatus

    You can get the value of CurrentStatus using the preceding syntax.


There is one parameter named IncludeUserName introduced with the Get-Process CMDLET in Windows PowerShell 4.0:

  • -IncludeUserName: This parameter includes a new column as UserName in the standard process object output.

    There is one limitation to this parameter; you can't use this along with the ComputerName parameter. If you want to do so, you need to use the following approach:

    PS C:\> Invoke-Command -ScriptBlock { Get-Process -IncludeUserName } -ComputerName PSTest


The Get-FileHash CMDLET has been newly introduced in Windows PowerShell 4.0. It gets a hash code for your specified file. This CMDLET comes very handy while comparing same files at different locations using hash tags. If you are copying large files such as ISO files from one location to other, you can verify the consistency with which this happens, whether the files are copied successfully or not, using this CMDLET. We have the freedom to use various algorithms to calculate the hash. The following command statement calculates the hash using the MD5 algorithm for one PowerShell script:

PS C :\> Get-FileHash -FilePath D:\SpaceAnalyser.ps1 -Algorithm MD5 | Format-List
Path : D:\SpaceAnalyser.ps1
Type : System.Security.Cryptography.MD5CryptoServiceProvider
Hash : h9uUHj8CGtGnV35reUkehw==