7

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?

Community
  • 1
  • 1
vmrob
  • 2,966
  • 29
  • 40
  • I've never seen that particular syntax before but it appears to do the same thing as %. Or at least that code works using % in place of the . – EBGreen Jun 15 '15 at 21:55

2 Answers2

9

. 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 }
Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • Breaking apart the code a bit, I ended up with an expression nearly identical to yours. Thanks for the explanation. – vmrob Jun 16 '15 at 16:21
4

. 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.

briantist
  • 45,546
  • 6
  • 82
  • 127