PowerShell: Is there an automatic variable for the last execution result?

前端 未结 10 1873
名媛妹妹
名媛妹妹 2021-02-05 00:29

I\'m looking for a feature comparable to Python interactive shell\'s \"_\" variable. In PowerShell I want something like this:

> Get-Something # this returns          


        
10条回答
  •  别那么骄傲
    2021-02-05 00:54

    Not exactly. There's the $_ automatic value which contains the current object in the pipe-line.

    The pipeline is the usual way to pass a result from one cmdlet to the next, and cmdlets are set to accept parameters from the pipeline or from the properties of objects in the pipeline making the use of a "last result" variable irrelevant.

    Still some situations do require a specific reference to the "piped" object and for those there's the $_ automatic value.

    Here's an example of its usage: Using the Where-Object Cmdlet and here's a list of powershell's automatic variables: Chapter 4. PowerShell Automatic Variables

    Scripting in powershell requires a different style than programming in Python (same as Python requires a different style than C++.)

    Powershell is built so pipes are used extensively, if you want to break up a pipeline into a more procedural step by step structure you'll need to save your results in named variables not automatic ones.

提交回复
热议问题