Conditionally specifying switch parameters in PowerShell Conditionally specifying switch parameters in PowerShell powershell powershell

Conditionally specifying switch parameters in PowerShell


There is one more way. You can use splatting like this:

$params = @{            Url = "$URL/$siteToCreate.url"            Name = $siteToCreate.name            Template = $webTemplate             Language = $siteToCreate.language }# if needed, add the keys to the hashtableif ($result) {    $params.AddToQuickLaunch = $true    $params.AddToTopNav = $true    $params.UseParentTopNav = $true}# now you just pass the parameters:New-SPWeb @params

More about splatting:

What does the "@" symbol do in Powershell?http://blogs.msdn.com/b/powershell/archive/2009/01/02/how-and-why-to-use-splatting-passing-switch-parameters.aspx


Calculate the $result ($true or $false) before:

$result = ...

And use it after:

New-SPWeb `-Url "$URL/$siteToCreate.url" `-Name $siteToCreate.name `-Template $webTemplate `-Language $siteToCreate.language `-AddToQuickLaunch:$result `-AddToTopNav:$result `-UseParentTopNav:$result