Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a default value for variable in Powershell [duplicate]

Tags:

powershell

In bash I can do something like this to set name to a default value if UserName is not set:

name=${UserName:-James}

Does Powershell support something like this?

like image 758
JohnnyLoo Avatar asked Aug 25 '16 21:08

JohnnyLoo


People also ask

How do I set default value in PowerShell?

You can use a script block to specify different default values for a parameter under different conditions. PowerShell evaluates the script block and uses the result as the default parameter value. The Format-Table:AutoSize key sets that switch parameter to a default value of True.

Does += work in PowerShell?

The equals sign ( = ) is the PowerShell assignment operator. PowerShell also has the following compound assignment operators: += , -= , *= , %= , ++ , -- , ??= . Compound assignment operators perform operations on the values before the assignment.

How do you assign default values to variables?

The OR Assignment (||=) Operator The logical OR assignment ( ||= ) operator assigns the new values only if the left operand is falsy. Below is an example of using ||= on a variable holding undefined . Next is an example of assigning a new value on a variable containing an empty string.

How do I assign a value to a variable in PowerShell script?

To create a new variable, use an assignment statement to assign a value to the variable. You don't have to declare the variable before using it. The default value of all variables is $null . To get a list of all the variables in your PowerShell session, type Get-Variable .


1 Answers

Using functions and parameters we can do something like what I believe you are doing.

A function example:

function WriteUser
{
    param($user = "A User",
    $message = "Message")
    Write-Host $user
    Write-Host $message
}

calling the function without parameters

WriteUser

will give you the output:

A User

Message

WriteUser -user "Me" -message "Error"

would write the following:

Me

Error

A couple extra notes, you do not have to use the parameter names.

WriteUser "Bob" "All Systems Go" would work by the order of the parameters.

You can switch the order of the named parameters as well:

WriteUser -message "Error" -user "user, user"

and the function will put the to the correct parameter.

Otherwise, I believe you would have to do something to approximate ternary behavior like:

function Like-Tern
{
    for ($i = 1; $i -lt $args.Count; $i++)
    {
        if ($args[$i] -eq ":")
        {
            $coord = $i
            break
        }
    }
    
    if ($coord -eq 0) { throw new System.Exception "No operator!" }
    $toReturn
    if ($args[$coord - 1] -eq "")
    {
        $toReturn = $args[$coord + 1]
    }
    else 
    {
        $toReturn = $args[$coord -1]
    }
    
    return $toReturn
}

Credit for the idea here A function file could also include:

Set-Alias ~ Like-Tern -Option AllScope

And then you would use it like:

$var = ~ $Value : "Johnny"

Of course, ~ was completely arbitrary because I couldn't get ${ to work...

like image 102
Austin T French Avatar answered Oct 24 '22 22:10

Austin T French