m.edmondson
m.edmondson

Reputation: 30872

Passing DateTime as a parameter

I've read widely that I can pass in an argument to my powershell script via:

param (
    [Datetime]$argument
)

I've also read that I can define a default value:

param (
    [Datetime]$argument = Get-Date "5/1/2006 7:00 AM"
)

However on doing this I get:

At C:\Users\medmondson\Desktop\Scripts\ScrumTimer.ps1:2 char:26 + [Datetime]$argument = Get-Date "5/1/2006 7:00 AM" + ~ Missing expression after '='. At C:\Users\medmondson\Desktop\Scripts\ScrumTimer.ps1:2 char:24 + [Datetime]$argument = Get-Date "5/1/2006 7:00 AM" + ~ Missing ')' in function parameter list. At C:\Users\medmondson\Desktop\Scripts\ScrumTimer.ps1:3 char:1 + ) + ~ Unexpected token ')' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingExpressionAfterToken

This only appears to occur for DateTime, any idea where I'm going wrong?

Upvotes: 4

Views: 14876

Answers (1)

CB.
CB.

Reputation: 60910

try enclose value in ()

param (
    [Datetime]$argument = (Get-Date "5/1/2006 7:00 AM")
)

Upvotes: 15

Related Questions