How can I change a variable based on user input, if no user input then put a default value in powershell?

Viewed 79

I'm new to powershell and am creating a script that will create a sharepoint site using user input. One of the variables is a Template. I want the script to say if there's no user input then the $Template variable will be a default value of "Team Site" otherwise if there is user input then use that.

I know my issue is with the "$Template = $TemplateInput" line but I don't know what to put here to grab the user input instead of just repeating the question. Maybe I'm overthinking this but I'm pretty sure this is just a loop.

Here's my code:

$TemplateInput =  Read-Host -Prompt "What sharepoint template should be used? Leave blank for default; Team Site"

if ($TemplateInput -eq $null) {
    $Template = "Team Site"
    Write-Host "Using Template: Team site"
    }
else {
        $Template = $TemplateInput
}

PAUSE
1 Answers

Note: I'm leaving aside that the user may have provided all-whitespace (one or more spaces) instead of no input. An easy way to treat all-whitespace as empty, and to generally remove incidental whitespace from the start and end of a string is to call a string's .Trim() method; e.g.:

$TemplateInput = (
  Read-Host -Prompt "What sharepoint template should be used? Leave blank for default; Team Site"
).Trim()

  • Don't compare string values in PowerShell to $null with -eq / -ne - strings in PowerShell are virtually never $null;[1]

    • If the user provides no interactive input to Read-Host (just presses Enter), the empty string is returned ('' or "", expressed as PowerShell literals) - never $null.

    • As an aside, re $var -eq $null in general: the form $null -eq $var - i.e. placing the comparison value on the LHS is preferable, given that PowerShell operators can act as filters with collections as LHS values, in which case $var -eq $null would return the sub-array of elements that aren't $null - see this answer.

  • If you know the value to test to be a string rather than $null, as in the case of Read-Host, use the following to test for an empty string:

    • if ('' -eq $TemplateInput)
  • In cases where a value could be $null (if the value isn't known to be [string]-typed or may be the return value from a string-typed .NET API that situationally returns a true $null):

    • Concise PowerShell-only, but slightly obscure solutions:

      • -not,[2] as Santiago Squarzon suggests:

         # Test for $null or '' (empty string).
         if (-not $TemplateInput) # ... 
        
      • Alternatively, -like ''[3]

         # Test for $null or '' (empty string).
         if ($TemplateInput -like '') # ... 
        
      • Or, to also cover the all-whitespace case, -notmatch '\S'[3] (\S is a regex escape sequence matching any non-whitespace char.):

         # Test for $null or '' or all-whitespace input.
         if ($TemplateInput -notmatch '\S') # ... 
        
    • .NET API solutions:

      • [string]::IsNullOrEmpty()

        # Test for $null or '' (empty string).
        if ([string]::IsNullOrEmpty($TemplateInput)) # ... 
        
      • Or, to also cover the all-whitespace case, [string]::IsNullOrWhiteSpace(), as Theo suggests:

        # Test for $null or '' or all-whitespace input.
        if ([string]::IsNullOrWhiteSpace($TemplateInput)) # ...
        

[1] Unless you go out of your way to use [string] $str = [NullString]::Value, which should only ever be used for .NET API calls. [string] $null -eq '' is $true (unlike $null -eq ''), because the [string] cast converts $null to ''. See this answer for more information.

[2] This takes advantage of PowerShell's implicit to-Boolean conversion logic: the use of the -not operator forces a string operand to be converted to a Boolean, and the empty string is considered $false, whereas any non-empty string - irrespective of its value - is $true; similarly, $null always converts to $false - see the bottom section of this answer for a general overview of PowerShell's to-Boolean conversions.

[3] This relies on the fact that -like and -match / -notmatch, as a string-only operators, coerce their LHS operand to a string, and $null is treated like '' in this conversion (verify with $null -like ''). Note that this amounts to a slight repurposing of the -like, whose purpose is to perform wildcard pattern matching. -match's purpose is to perform regex matching.

Related