Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8762705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:35:55+00:00 2026-06-13T15:35:55+00:00

Possible Duplicate: What does $_ mean in powershell? I have already seen someone else’s

  • 0

Possible Duplicate:
What does $_ mean in powershell?

I have already seen someone else’s question and answer on the same subject but for me it does not answer the question.

I know how to use Select-Object, Where-Object, ForEach-Object etc and pass a script block in, for example:

dir | ForEach-Object { $_.Name }

which returns an array of all file names in the current directory.

This is similar to C#’s Select extension method:

Dir.Select(x => x.Name)

However, the above C# is easily understandable because we are passing a function to the Select method which then executes it for each element in the list.

However, the equivalent in PowerShell:

dir | ForEach-Object { param($x) $x.Name }

does not return anything. However

& { param($x) $x.Name } (dir)[0]

works as expected.

Can someone please explain the magic of $_ to someone familiar to C# but not the world of scripting? It is a special keyword? Can it be only be used in script blocks which are passed into cmdlets? How do you invoke one without piping one to ForEach-Object (or any other cmdlet)? i.e:

& { $_.Name } (dir)[0]

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:35:57+00:00Added an answer on June 13, 2026 at 3:35 pm

    The $_ is only created in the context of a pipeline, that is the passing of output objects from one command into the “input” of another. For pipeline filters (which are like functions), powershell automatically populates the $_ variable with the current object of the pipeline. Now I say current because it is important to note that when multiple objects are passed down the pipeline, the process scriptblock of your filter is executed once per object.

    Now, you could rename the the pipeline objects to one of you input parameters, but you have to tell powershell that you are doing so. For e.g.:

    # C:\> filter get-name-filt {Param([parameter(ValueFromPipeline=$true)]$x) $x.name}
    # C:\> (dir)[0] | get-name-filt
    

    which is not terribly convenient on the command line, hence the $_ automatic variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What does it mean to have an undefined reference to a static
Possible Duplicate: Reference - What does this symbol mean in PHP? I have a
Possible Duplicate: what does %.*s mean in printf in c I know this question
Possible Duplicate: What does jQuery.fn mean? I have some script on a page trying
Possible Duplicate: What does dot (.) mean in a struct initializer? I have the
Possible Duplicate: What does ||= mean? In this previous question, I asked about an
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: What does T&& mean in C++0x? I had never seen a double
Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript I have the following
Possible Duplicate: What does this construct (x = x || y) mean? I've seen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.