Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from   Instant Windows PowerShell Guide Enhance your knowledge of Windows PowerShell and get to grips with its latest features

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Length 86 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Harshul Patel Harshul Patel
Author Profile Icon Harshul Patel
Harshul Patel
Arrow right icon
View More author details
Toc

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:

Export-Csv

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.

Add-Member

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.

    Tip

    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
    Completed
    

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

Get-Process

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
    

Get-FileHash

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==
You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013
Publisher: Packt
ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image