Why we write as $_. why not simply $_

Welcome Forums General PowerShell Q&A Why we write as $_. why not simply $_

This topic contains 8 replies, has 7 voices, and was last updated by

 
Participant
1 month, 1 week ago.

  • Author
    Posts
  • #119553

    Participant
    Points: 55
    Rank: Member

    Hi Gents,

    I'm a beginner to Powershell world. I understand $_ represents the objects piped in to the pipeline. But why there is '.' after '$_' For example why we need to write Get-Service | Where-Object {$_.status -eq 'running'} why not 'Get-Service | Where-Object {$_status -eq 'running'}' ?? Expecting a brief explanation. Thanks.

  • #119569

    Moderator
    Points: 99
    Team MemberHelping Hand
    Rank: Member

    To put it simply, that's just how you access object properties in PowerShell –

    `$_` (and `$PSItem`, which can be used interchangeably) are a way to access the pipeline object.

    When accessing properties, you use dot-notation (e.g. `$Object.PropertyName`) to do so.

    `$_` is just a variable name, the same as `$Object`, so you use `$_.PropertyName` to access the properties.

    The thing to bear in mind there is that if you access `$_status`, you're actually accessing the variable named "_status" which (probably) doesn't exist.

  • #119571

    Participant
    Points: 111
    Helping Hand
    Rank: Participant

    I would look at it like this:

    $spooler = Get-Service -Name spooler

    Why do you write $spooler.Status to get the status of the spooler service, and not $spoolerstatus?

  • #119575

    Participant
    Points: 815
    Helping Hand
    Rank: Major Contributor

    Well, simple answer is ,

    This is how the language is designed

    $_ is the current element in the pipeline and to use the members of each of them, we use dot(cherry picking). This is how the whole world of Dot net is nad PowerShell is build on top of .net. And $_Status is considered as a variable by the parser as it starts with $ and continues till a space/line break/= etc... by following criteria for a variable name.

    $_status = 'somevalue'
    $_CompanyName = 'company1'
    
  • #119578

    Participant
    Points: 190
    Helping Hand
    Rank: Participant

    Maybe someone has a more academic explanation.
    But it's from what I can tell from the coding languages I've tested the common way to state which value you want from an object.
    Remember sometimes it's not just one layer, you could have multiple/nested values in an object.
    So you would still need something to delimit which value you want further down.

    E.g. $_.ValueX.ValueY

  • #119581

    Participant
    Points: 41
    Rank: Member

    Hi,

    powershell is a object oriented shell , it's not text oriented like unix shells.

    Every cmdlet outputs objects  ,use get-member to inspect cmdlets output :

    get-service | get-member

     

     

  • #119593

    Participant
    Points: 27
    Rank: Member

    Hi,

    I was taught in this way. Might be easy to remember for you.

    Whenever you use any cmdlet, it produces a table inside the memory. For example, Get-Service will create a table which contains a collection of service objects and its properties. Now $_ represent the entire table header. So if you use $_, POSH will understand that you are trying to use/call the entire table. But, if you want to call a particular column from that table, you have to use $_.column name (Ex. $_.Status). (. dot) used for mentioning a column. Without this POSH will not understand.

    Hope it'll help.

    Regards

    Roy.

  • #120412

    Participant
    Points: 55
    Rank: Member

    Thanks Guys. I'm good now!

You must be logged in to reply to this topic.