Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explanation should be refined for '$_' in scriptblocks without pipeline. #87

Closed
LaurentDardenne opened this issue Jul 1, 2022 · 0 comments

Comments

@LaurentDardenne
Copy link

www.explainpowershell.com considers the automatic variables '$_' and '$PsItem' as linked to a pipeline.
For the following examples this is not the case, at least not in the submitted code because the pipe character '|' does not exist.
A beginner may therefore be disappointed by the explanation given so far.
With :

switch (get-service | select-object status) { {$_.status -like "Running"} {$running++}}

From the PS v3 specification document (8.6 The switch statement):
Switch :

on entry to each statement-block, $_ is automatically assigned the value of the switch-condition that caused control to go to that statement-block.
$_ is also available in that statement-block's switch-clause-condition.

For throw :

Within a matching catch clause, the variable $_ contains a description of the current exception.

For trap {"Error found: $psitem"}

Within a trap statement the variable $_ contains a description of the current error.

There are also Winform and WPF eventhandlers, but that's off topic here :

$Form1.Add_FormClosing( { 
	# $this = sender (object)
	# $_ =  e (eventarg)

	($_).Cancel= $False
} )

Note :
The constructions submitted here are suitable because only one line can be entered.

@Jawz84 Jawz84 changed the title One remark about automatic variable pipeline Explanation should be refined for '$_' in scriptblocks without pipeline. Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant