Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Powershell, what is $input for? [duplicate]

Tags:

powershell

I was trying to create a hash table,

$input = @{'G'=100;'E'=50;'D'=35;'A'=100}

and could not figure out for the life of me why it wouldn't display as usual with commands like write-host, or simply $input. write-host returned System.Collections.ArrayList+ArrayListEnumeratorSimple. $input returned nothing. No error was thrown.

On a hunch I renamed the hash table, and boom, it appears as normal. Opening a new powershell tab in ISE, I observe that the variable $input is filled in with intellisense even though I have not defined it in this environment.

Now I'm curious: what is this system variable $input for? I'm on version 4.

like image 486
Mr.Budris Avatar asked Apr 05 '17 20:04

Mr.Budris


People also ask

What is the input and output of PowerShell?

Input : 'Geeks for Geeks' Output : Hello Geeks for Geeks Input : 'PowerShell Beginner' Output : Hello PowerShell Beginner In Windows Powershell, the input and output are given through the Host. It uses ‘Write-Host’ to print and ‘Read-Host’ to get input from console. Function to get user input.

How to get the input and output from the console?

In Windows Powershell, the input and output are given through the Host. It uses ‘Write-Host’ to print and ‘Read-Host’ to get input from console. Example 2: Taking the User input through console Function to get user input. Input : 'Geeks for Geeks' Output : Hello Geeks for Geeks Input : 'PowerShell Beginner' Output : Hello PowerShell Beginner

What is the use of PowerShell in Windows?

Windows PowerShell is a command-line and scripting language designed for system administration. It helps IT professionals, to control and automate the administration of the Windows Operating System and Windows Server Environment. There are cmdlet for console input and output using PowerShell.

How do I get dynamic input in PowerShell?

Often times your PowerShell code will need to evaluate some type of input. That input can be statically provided by you ahead of time. There are often use cases though where retrieving a dynamic input is desired. You can of course use the output of cmdlets for the input of your PowerShell functionality.


1 Answers

It's an automatic variable:

$INPUT

Contains an enumerator that enumerates all input that is passed to a function. The $input variable is available only to functions and script blocks (which are unnamed functions). In the Process block of a function, the $input variable enumerates the object that is currently in the pipeline. When the Process block completes, there are no objects left in the pipeline, so the $input variable enumerates an empty collection. If the function does not have a Process block, then in the End block, the $input variable enumerates the collection of all input to the function.

This is also available in PowerShell:

Get-Help about_Automatic_Variables

I also have an open feature request for Set-StrictMode to handle detection of this.

like image 161
briantist Avatar answered Nov 07 '22 06:11

briantist