Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the `.` shorthand for in a PowerShell pipeline?

Tags:

powershell

I'm looking over a block of code I've used (sourced from another question) and I haven't been able to figure out what the . in .{process represents in this snippet (comments removed):

Get-ItemProperty $path |
.{process{ if ($_.DisplayName -and $_.UninstallString) { $_ } }} |
Select-Object DisplayName, Publisher, InstallDate, DisplayVersion, HelpLink, UninstallString |
Sort-Object DisplayName

I know that % is For-EachObject and ? is shorthand for Where or Where-Object, but the question remains:

What is . shorthand for?

like image 377
vmrob Avatar asked Jun 15 '15 21:06

vmrob


People also ask

What is the symbol for pipeline in PowerShell?

The pipeline character in Windows PowerShell is the vertical bar (also called the pipe: | ). On most U.S. keyboards, it is found on the key with the backslash.

What is $? In PowerShell?

$? Contains the execution status of the last command. It contains True if the last command succeeded and False if it failed. For cmdlets and advanced functions that are run at multiple stages in a pipeline, for example in both process and end blocks, calling this.

What does @{} mean in PowerShell?

The Splatting Operator To create an array, we create a variable and assign the array. Arrays are noted by the "@" symbol.

What does += in PowerShell mean?

The assignment by addition operator += either increments the value of a variable or appends the specified value to the existing value. The action depends on whether the variable has a numeric or string type and whether the variable contains a single value (a scalar) or multiple values (a collection).


2 Answers

. is the dot sourcing operator, which runs a script in the current scope rather than a new scope like call operator (i.e. &).

That second segment invokes a script block and in that script block defines an advanced function. The advanced function iterates each item in the pipeline and selectively passes it along.

This is not really an idiomatic use. What this script is trying to achieve could be done in a simpler, more readable way by using Where-Object (often shortened to where or ?):

Get-ItemProperty $path | where { $_.DisplayName -and $_.UninstallString }
like image 79
Mike Zboray Avatar answered Oct 30 '22 17:10

Mike Zboray


. is the dot source operator. I've never seen it used quite this way, but it's identical to using & (the call operator) in this context.

like image 41
briantist Avatar answered Oct 30 '22 18:10

briantist