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

4 months, 3 weeks ago.

  • Author
  • #119553

    Points: 67
    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

    Points: 182
    Team MemberHelping Hand
    Rank: Participant

    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

    Points: 117
    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

    Points: 1,628
    Helping Hand
    Rank: Community Hero

    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

    Points: 364
    Helping Hand
    Rank: Contributor

    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

    Points: 41
    Rank: Member


    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

    Points: 70
    Rank: Member


    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.



  • #120412

    Points: 67
    Rank: Member

    Thanks Guys. I'm good now!

The topic ‘Why we write as $_. why not simply $_’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort