Powershell Cmdlet with Mandatory Parameters

Blazey picture Blazey · Jun 22, 2011 · Viewed 8k times · Source

I'm trying to create a simple powershell cmdlet that would have a few mandatory parameters. I've found the following code for doing so however, I cannot get it to execute:

function new-command() {
    [CmdletBinding()]
    PARAM (
        [Parameter(Mandatory=$true)]
        [string]$Name
    )
}

new-command

Returns the following error:

Missing closing ')' in expression." Line: 5 Char: 3 + [ <<<< string]$Name

What am I doing wrong?

Answer

JPBlanc picture JPBlanc · Jun 22, 2011

The explanation is that you are running this script in PowerShell V1.0 and these function attributes are supported in PowerShell V2.0. Look at $host variable for you PowerHhell version.