Josiah
Josiah

Reputation: 2866

How can I set the default argument of a function to the output of another function in PowerShell 2?

In PowerShell 2 I have a function Get-CurrentQuarter that returns the current quarter.

I have another function that takes the parameter quarter. I'd like it to default to the current quarter using the function Get-CurrentQuarter.

I tried:

function Test-ParameterByFunction
{
  param(
    [string]$quarter = Get-CurrentQuarter
  )
}

Test-ParameterByFunction

PowerShell reported:

PS > .\test.ps1
Missing expression after '='.
At .\test.ps1:4 char:23
+     [string]$quarter = <<<<  Get-CurrentQuarter
    + CategoryInfo          : ParserError: (=:String) [], ParseException
    + FullyQualifiedErrorId : MissingExpressionAfterToken

This would be really handy in cleaning up my code. Is there some syntax I'm missing?

Thanks!

Upvotes: 0

Views: 568

Answers (1)

Dave Sexton
Dave Sexton

Reputation: 11188

Try this:

[string]$quarter = [string](Get-CurrentQuarter)

Upvotes: 5

Related Questions