Punctuation Help: $_. AND Dot Operator AND Fallback Dot Operator

I finally understand this code pasted below from

Learn Windows PowerShell in a Month of Lunches Third Edition, Page 114

But I have seen other code like this, directly below. What’s that Dot? Or Period?

Is it a Dot Operator? Is it a Fallback Dot Operator?

Where can I read about how this works?

 $names | foreach { "$_ = $($obj.$_)" }

And

$names = ( $obj | Get-Member -Type property l*).name

And

 $names.foreach{ "$_ = $($obj.$_)" }

The above is from Windows Powershell In Action, Third Edition, P. 136-137

Code pasted below is from:

Learn Windows PowerShell in a Month of Lunches Third Edition, Page 114

Input CSV File

login dept city title

DonJ IT Las Vegas CTO

Gregs Custodial Denver Janitor

JeffM IT Syracuse Network Engineer

I understand the $. very well from the book. Does this $. have a name? I want to read more about it somewhere.

PS C:\WINDOWS\system32> import-csv c:\Homeuse\newusers.csv |
select-object -property *,
@{name='samAccountName';expression={$_.login}},
@{label='Name';expression={$_.login}},
@{n='Department';e={$_.Dept}}

login : DonJ
dept : IT
city : Las Vegas
title : CTO
samAccountName : DonJ
Name : DonJ
Department : IT

login : Gregs
dept : Custodial
city : Denver
title : Janitor
samAccountName : Gregs
Name : Gregs
Department : Custodial

login : JeffM
dept : IT
city : Syracuse
title : Network Engineer
samAccountName : JeffM
Name : JeffM
Department : IT

The dot (.) in ALL your examples is technically called the “property dereference operator”. See Get-Help About_Operators. I have searched the documentation and can’t find any reference to a “fallback Dot Operator”. What do you mean by this term?

The $_ is technically the same as $PSItem. $PSItem is an automatic variable containing the current pipeline object. See Get-Help About_Automatic_Variables. In your foreach examples regardless if it is the pipeline to the foreach-object loop or the foreach method of an array object, the $PSItem or $_ will always represent the current object in the iteration.

As for as combining the two concepts ($PSItem or $_ and the property dereference operator), the dot operator is simply used to call a property or method of the current pipeline object. Hopefully this helps

Hi,

First, you can read more about it here https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.1

Let’s agree about the $ which represents a variable. But when it comes to a pipeline where there is a stream of data passed through the pipeline, each item in this pipeline is considered as a variable and it’s referred to as $_ which means an item from the pipeline which may include an array or whatever.

In order to reach for these item properties, a Dot is used, just like a regular PowerShell the Dot is a call for a method or property related to the item

Let’s have some example

Regular variable

$Mylist=Get-Process notepad #Now the Mylist Variable will hold all the information related to the Notepad process

 

Let’s try now to pass this information to Write-host and add a basic format color Green

($Mylist).foreach{Write-Host $_ -ForegroundColor green}

In the example above $Mylist hold the information of Notepad process and its passed to a Foreach Loop. As you can read the usage of the $_ represent a single item from the $MyList variable

The output of the above line is Notepad formated in the green color.

Dot Format

Now let think that what we need is not the Object itself NOTEPAD Process but a property of the object, for example, ProcessID, By adding a Dot to $_ we will be able to tell Powershell that we need property from the pipeline item named ID

($Mylist).foreach{Write-Host $_.ID -ForegroundColor green}

The $_ is referring to the object that reaches through the Pipeline, and the DOT after $_ refer to the property of the item

ITEM Property

You can get more information about the item property by adding | gm to any object.

I hope I make things a bit simpler.

MikeR, Thank you for your reply. I got Dot Operator and FallBack Dot Operator from a Book: Windows Powershell In Action, Third Edition, Bruce Payette & Richard Siddaway, P. 132 and Page 134

2 PowerShell Features I was Surprised to Love - PowerShell Station

So the dot is used with the Pipeline, you say. I will go study your reference, Get-Help About_Operators. this but it will take some time before I get back to you.

 

Faris, Thank you for your reply, Yes, indeed, I will go study what you say: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.1

I am liking your examples, You said: “…the Dot is a call for a method or property related to the item.” I will go study this. I will have to run some examples in PowerShell and examine everything and learn it.

I will be most interested to read the rules about this.

I will get back after I have studied, but it will take some time, I am working about 12 hours every day.

Thanks, guys.