PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

Geek Sync Live

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.

Latest Forum Posts

02-10-2016 - IIS Information from Network Servers
02-09-2016 - Start-Job Large File
02-09-2016 - Add comma in Excel

As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Magic Underscore Variable

Here is a very special (and very underdocumented) way to use PowerShell parameters. Have a look at this function:

#requires -Version 2

function Test-DollarUnderscore
{
  param
  (
    [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [string]
    $Test
  )

  process
  {
    "received: $Test"
  }
}

It does not seem to be very unusual at first. You can assign values to the -Test parameter, and the function returns them:

 
PS C:\> Test-DollarUnderscore -Test 'Some Data'
received: Some Data 
 

But now check out what happens when you pipe data into the function:

 
PS C:\> 1..4 | Test-DollarUnderscore -Test { "I am receiving $_" }
received: I am receiving 1
received: I am receiving 2
received: I am receiving 3
received: I am receiving 4 
 

The -Test parameter suddenly and automagically accepts script blocks (although the assigned type was a string), and inside of the script block, you have access to the incoming pipeline element.

You get this very special parameter support when you set ValueFromPipelineByPropertyName=$true with a mandatory paramater, and the incoming data has no property that matches the parameter.

 

Throughout this month, we'd like to point you to two awesome community-driven global PowerShell events taking place this year:

Europe: April 20-22: 3-day PowerShell Conference EU in Hannover, Germany, with more than 30+ speakers including Jeffrey Snover and Bruce Payette, and 60+ sessions (www.psconf.eu).

Asia: October 21-22: 2-day PowerShell Conference Asia in Singapore. Watch latest annoncements at www.psconf.asia

Both events have limited seats available so you may want to register early.

Twitter This Tip! ReTweet this Tip!

Copyright 2012 PowerShell.com. All rights reserved.