简体   繁体   English

$_ 在 PowerShell 中是什么意思?

[英]What does $_ mean in PowerShell?

I've seen the following a lot in PowerShell, but what does it do exactly?我在 PowerShell 中看到了很多以下内容,但它到底做了什么?

$_

This is the variable for the current value in the pipe line, which is called $PSItem in Powershell 3 and newer. 这是管道中当前值的变量,在Powershell 3和更新版本中称为$PSItem

1,2,3 | %{ write-host $_ } 

or 要么

1,2,3 | %{ write-host $PSItem } 

For example in the above code the %{} block is called for every value in the array. 例如,在上面的代码中,为数组中的每个值调用%{}块。 The $_ or $PSItem variable will contain the current value. $_$PSItem变量将包含当前值。

I think the easiest way to think about this variable like input parameter in lambda expression in C#. 我认为考虑这个变量的最简单方法就像在C#中的lambda表达式中的输入参数一样。 Ie $_ is similar to x in x => Console.WriteLine(x) anonymous function in C#. $_类似于x中的x => Console.WriteLine(x) C#中的x => Console.WriteLine(x)匿名函数。 Consider following examples: 考虑以下示例:

PowerShell: 电源外壳:

1,2,3 | ForEach-Object {Write-Host $_}

Prints: 打印:

1
2
3

or 要么

1,2,3 | Where-Object {$_ -gt 1}

Prints: 打印:

2
3

And compare this with C# syntax using LINQ: 并使用LINQ将其与C#语法进行比较:

var list = new List<int> { 1, 2, 3 };
list.ForEach( _ => Console.WriteLine( _ ));

Prints: 打印:

1
2
3

or 要么

list.Where( _ => _ > 1)
    .ToList()
    .ForEach(s => Console.WriteLine(s));

Prints: 打印:

2
3

According to this website, it's a reference to this , mostly in loops. 根据这个网站,这是一个参考this ,主要是在循环。

$_ (dollar underscore) 'THIS' token. $ _(美元下划线)'这个'代币。 Typically refers to the item inside a foreach loop. 通常是指foreach循环内的项目。 Task: Print all items in a collection. 任务:打印集合中的所有项目。 Solution. 解。 ... | ...... | foreach { Write-Host $_ } foreach {Write-Host $ _}

$_ is an alias for automatic variable $PSItem (introduced in PowerShell V3.0; Usage information found here ) which represents the current item from the pipe. $ _是自动变量$ PSItem的别名(在PowerShell V3.0中引入; 此处的用法信息 ),表示管道中的当前项。

PowerShell (v6.0) online documentation for automatic variables is here . PowerShell(v6.0)自动变量的在线文档就在这里

$_ is an variable which iterates over each object/element passed from the previous | $ _是一个变量,它迭代从前一个|传递的每个对象/元素 (pipe). (管)。

$_ is a variable created by the system usually inside block expressions that are referenced by cmdlets that are used with pipe such as Where-Object and ForEach-Object . $_是由系统创建的变量,通常在块表达式中,由与管道一起使用的cmdlet引用,例如Where-ObjectForEach-Object

But it can be used also in other types of expressions, for example with Select-Object combined with expression properties. 但它也可以用在其他类型的表达式中,例如将Select-Object与表达式属性结合使用。 Get-ChildItem | Select-Object @{Name="Name";Expression={$_.Name}} Get-ChildItem | Select-Object @{Name="Name";Expression={$_.Name}} . Get-ChildItem | Select-Object @{Name="Name";Expression={$_.Name}} In this case the $_ represents the item being piped but multiple expressions can exist. 在这种情况下, $_表示正在管道的项目,但可以存在多个表达式。

It can also be referenced by custom parameter validation, where a script block is used to validate a value. 它也可以通过自定义参数验证来引用,其中脚本块用于验证值。 In this case the $_ represents the parameter value as received from the invocation. 在这种情况下, $_表示从调用接收的参数值。

The closest analogy to c# and java is the lamda expression. 与c#和java最接近的类比是lamda表达式。 If you break down powershell to basics then everything is a script block including a script file a, functions and cmdlets. 如果你将powershell分解为基础知识,那么一切都是脚本块,包括脚本文件a,函数和cmdlet。 You can define your own parameters but in some occasions one is created by the system for you that represents the input item to process/evaluate . 您可以定义自己的参数,但在某些情况下,系统会为您创建一个表示要处理/评估的输入项的参数 In those situations the automatic variable is $_ . 在这些情况下,自动变量是$_

The $_ is a $PSItem, which is essentially an object piped from another command. $_ 是一个 $PSItem,它本质上是一个从另一个命令通过管道传输的对象。 For example, running Get-Volume on my workstations returns Rows of PSItems, or objects例如,在我的工作站上运行 Get-Volume 会返回 PSItems 行或对象

get-volume | select driveLetter,DriveType   

driveLetter DriveType
----------- ---------
      D      Fixed
             Fixed
      C      Fixed
      A      Removable

Driveletter and DriveType are properties Now, you can use these item properties when piping the output with $_.(propertyName). Driveletter 和 DriveType 是属性 现在,您可以在使用 $_.(propertyName) 管道输出时使用这些项属性。 (Also remember % is alias for Foreach-Object) For example (还要记住 % 是 Foreach-Object 的别名)例如

$vol = get-volume | select driveLetter,DriveType

$vol | Foreach-Object {
    if($_.DriveType -eq "Fixed") {
        "$($_.driveLetter) is $($_.driveType)"}
     else{
        "$($_.driveLetter) is $($_.driveType)"
     }
 }

Using Terinary in Powershell 7, I am able to shorten the logic while using properties from the Piped PSItem在 Powershell 7 中使用 Terinary,我能够在使用 Piped PSItem 中的属性时缩短逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM