Powershell Features

PowerShell Features

PowerShell Features Include :

  • Cmdlets
  • Pipeline
  • Providers
  • Help system

Cmdlets

Cmdlets are probably the most obvious feature when comparing PowerShell to other scripting languages. A cmdlet is a small, self contained piece of functionality that does one specific job.

Get-Command – Gets all commands installed on the computer, including cmdlets, aliases, functions, filters, scripts, and applications. Get-Command gets the commands from PowerShell modules and commands that were imported from other sessions

CASES AND OPERATORS

PowerShell isn’t case sensitive. It can be written in all lowercase, all uppercase, or any random combination. Best practice id to follow the style of PowerShell itself when capitalizing cmdlet names, properties, or methods.

cmdlets has a verb-noun syntax

Example : Get-Service
Verb – Get
Noun – Service

Cmdlet names should always be singular, so use Get-Service rather than Get-Services

count the number of cmdlets

Second cmdlet, Where-Object (utility cmdlets) functions as a filter acting on the information moving along the pipeline. The filter determines whether the PSSnapin property is like the string Microsoft.P*, where * is the usual wildcard character. Note the use of {} to enclose the script block that provides the filtering. By wrapping the cmdlets in (), we can treat the results as a collection of objects and use the Count property to determine the number of cmdlets present that match the filter.

Advertisements
Advertisements

TAB COMPLETION

If you type Get- at the command line, then press the Tab key, the PowerShell engine will complete the command with the first cmdlet that matches what’s been typed so far. In this case, it’s usually Get-Acl. If the Tab key is pressed again, the next Get- cmdlet will be displayed, and repeated pressing of the Tab key enables you to cycle through the list of relevant cmdlets. Tab completion can be invoked from any relevant part of the cmdlet name, so for instance Get-C followed by Tab starts cycling through the Get cmdlets whose noun part starts with C.

Tab completion also applies to parameters, in that typing—followed by the Tab key enables you to cycle through the parameter list. As with the cmdlet names, the more of the parameter name you give, the sooner the process brings the required parameter.

ALIASES

As an alternative to typing the full name of a cmdlet or parameter, it’s possible to use an alias. An alias is shorthand for the command. Aliases can be used at the command line as well as in scripts. The use of aliases saves on typing, but at the expense of readability. The list of standard aliases is provided in appendix A. It’s also possible to create your own aliases using the Set-Alias cmdlet

As an alternative to typing the full name of a cmdlet or parameter, it’s possible to use an alias. An alias is shorthand for the command. Aliases can be used at the command line as well as in scripts. The use of aliases saves on typing, but at the expense of readability. The list of standard aliases is provided in appendix A. It’s also possible to create your own aliases using the Set-Alias cmdlet.

PARAMETERS

PowerShell cmdlets have parameters to define the input and possibly output, or to select various options. Parameters are always preceded by a hyphen. The parameters of a particular cmdlet can be viewed by using Get-Help. Using a command such as Get-Help Get-WmiObject –full will display the parameters of Get-WmiObject as well as the other help information. Typing Get-Help Get-WmiObject –parameter * will display only the parameters. As an example, consider the Class parameter from Get-WmiObject

-Class [<string>]
Specifies the name of a WMI class. When this parameter is used, the cmdlet retrieves instances of the WMI class.

Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false

Required? – whether the parameter is considered mandatory for that cmdlet, with the value given as true or false. If the parameter is mandatory and isn’t supplied, PowerShell will prompt for the value.

Position? – option indicates whether data can be passed to the cmdlet and be automatically allocated to the parameter. In this case, the first argument passed to the cmdlet is assumed to be the WMI class to retrieve. If the data doesn’t represent a valid WMI class, an error will be thrown. If a value of named or 0 is given here, it means that the parameter name must explicitly be used.

Default value indicates whether a default value has been set. If the data required by a parameter can be accepted from the pipeline.

Accept pipeline input? will be set to true.

Accept wildcard characters? option will be set to true if wildcards can be used in the input.

Advertisements
Advertisements

Common cmdlet parameters

-Debug Displays detailed information useful to programmers.
-ErrorAction Indicates how the cmdlet responds to a non terminating error. Possible values are Silently Continue, Continue, Inquire, Stop.
-ErrorVariable Stores information about errors in the specified variable.
-OutBuffer Determines the number of objects to store before sending them onto the pipeline. This is usually omitted, which means that objects are sent onto the pipeline immediately.
-OutVariable Stores error messages in the specified variable.
-Verbose Displays detailed information about the operation.

Safety parameters

-WhatIf present, this parameter causes PowerShell to output a list of statements indicating what would’ve happened if the command had been executed, without executing the command.
-Confirm Prompts the user for confirmation before performing any action.

Further information can be found using Get-Help about_CommonParameters

Pipeline

The ability to pipe data from one command to another.

Get-Process | Where-Object {$_.Handles -gt 500} |
Sort Handles | Format-Table

This example shows a Get-Process cmdlet passing data along the pipeline to a Where-Object cmdlet. The Get-Process cmdlet passes one .NET object for each process that’s present on the machine. A filter is applied to only accept processes that use more than 500 handles. The objects representing the processes are sorted by the number of handles and finally displayed in a table.

Using Get-Member to view the .NET type
TypeName: System.Diagnostics.Process
Name      MemberType            Definition
----      ----------            ----------
Handles  AliasProperty       Handles = Handlecount
Name     AliasProperty        Name = ProcessName
.
.
Kill       Method             System.Void Kill()
Id        Property          System.Int32 Id {get;}
.
.
...Listing truncated for brevity

The use of Get-Member shows that the Get-Process cmdlet is producing, or emitting, .NET objects of type System.Diagnostics.Process. This .NET type has a property called Handles. The Where-Object cmdlet performs a filtering operation based on the value of the Handles property of each .NET object. Any object that has a value greater than 500 for the Handles property is passed. All other objects are filtered out. The symbol $_ is used in PowerShell to refer to the current object being passed along the pipeline

A number of cmdlets, including the Format- and Write- cmdlets, will terminate the pipeline in that the objects cannot be passed to another cmdlet. If a Foreach-Object cmdlet is used, it’s perfectly valid to create a pipeline within the loop produced by that cmdlet.

The data that Get-Process produces is as of the time of execution. When investigating a set of data such as that referring to the running processes, it’s sometimes necessary to ensure that all comparisons are performed on exactly the same data

$proc = Get-Process
$proc | Where-Object{$_.Handles -gt 500}
$proc | Where-Object{$_.CPU -gt 100}
$proc | Sort-Object -Property WS -Descending |Select-Object -First 5

For more information on the pipeline, type Get-Help about_pipeline

Utility cmdlets and their purposes

Advertisements
Advertisements

Providers

Have you ever wanted a consistent method of working with multiple data stores such as the filesystem, Active Directory, SQL Server, IIS, and the Windows Registry? Power-Shell can deliver a large part of that vision through the use of providers.

The provider feature in PowerShell gives us a way of treating data stores as if they were the filesystem. PowerShell demonstrations where we do a dir through Active Directory or the Registry always go down well. The provider exposes a data store as just another drive on your system. Listing 1.9 shows how to view the installed providers and the associated drives. Note that the cmdlet refers to them as PSDrives to differentiate them from physical drives.

Help system

PowerShell has a set of help files that are presented in the shell as text files when you use Get-Help. The help system will be covered in detail in chapter 2 when we look at learning PowerShell

Advertisements