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.
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
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, theOut-GridView
CMDLET doesn't generate output objects except for an interactive console. Using theOutputMode
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 thePassThru
parameter.
-PassThru [<SwitchParameter>]
: This parameter acts like theOutputMode
parameter with theMultiple
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, theOut-GridView
CMDLET generates an interactive window with a complete command statement as the title of the window. We can set the title manually using theTitle
parameter along with theOut-GridView
CMDLET.-Wait [<SwitchParameter>]
: By default, with the execution of theOut-GridView
CMDLET, the console prompt returns immediately. You can explicitly prevent the command prompt to return immediately with theWait
parameter along with theOut-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, theExport-Csv
CMDLET overwrites the output to the specified file if it is already available in the defined location. Using theAppend
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 theAdd-Member
CMDLET.-NotePropertyName <String>
: This parameter passes property names to theAdd-Member
CMDLET.-NotePropertyValue <Object>
: This parameter passes a value object to property names that are defined with theNotePropertyName
parameter.Tip
It is recommended to use
NodePropertyName
andNodePropertyValue
together to provide custom properties with theAdd-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 valueCompleted
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 asUserName
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==