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

image rotation

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

Latest Scripts

02-27-2015 - Get-UserLogonTime
02-25-2015 - Expand-LinkPath
02-18-2015 - Search Script for Commands from a specific Module

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

Using Arrays as Parameter Default Values

PowerShell 3.0 and later

If you define a PowerShell function with some parameters, and you want a given parameter to have a default value that is an array, you may run into a syntax problem:

function Get-SomeData
{
  param
  (
    $ServerID = 1,2,5,10,11
  )

  "Your choice: $ServerID"
}

PowerShell uses commas to separate parameters, so the comma after the number “1” in your param() block is misinterpreted, and PowerShell thinks that a new parameter definition follows.

Whenever there is room for misinterpretation, use parenthesis to make sure which parts belong together. This is perfect valid code:

function Get-SomeData
{
  param
  (
    $ServerID = (1,2,5,10,11)
  )

  "Your choice: $ServerID"
} 

Twitter This Tip! ReTweet this Tip!

Copyright 2012 PowerShell.com. All rights reserved.