PowerShell $_ Pipeline Variable Tutorial

PowerShell provides variables in order to store data which can be text, number, etc. PowerShell also provides some special variables which are used with PowerShell structures. The $_ dollar sign and underscore is used to define a special variable called Pipeline Variable . In PowerShell, the $ sign is used to define variables where a variable starts with a dollar sign.

$_ Syntax

The Pipeline variable has the following simple syntax where it is used as a variable and different attributes or methods can be called.

  • ATTRIBUTE can be an attribute or method which belongs to $_ variable.

Pipe Object To Variable

Let’s start with a simple example where we pipe an object into the $_ . The Where-Object or similar statements are used to get piped object like below.

PS> Get-Host | Where-Object {$_}

Filter Pipeline Variable

One of the most powerful feature of the pipeline variable is it can be filtered easily for multiple objects provided via the pipe. The filter can be done according to the specific field of the object by using the -Match option. In the following example we use the Get-Service command which lists all services and filter according to their names which contains win . Simply only services those names contain “win” are listed.

PS> Get-Service | Where {$_.DisplayName -Match "win"}
Filter Pipeline Variable

Leave a Comment