New-Variable

Create a new variable.

Syntax
      New-Variable [-Name] String [[-value] Object]
           [-scope string] [-description string] [-Visibility {Public | Private}]
              [-option {None | ReadOnly | Constant | Private | AllScope}]
                 [-force] [-passThru] [-whatIf] [-confirm]
                    [CommonParameters]
Key
   -Name 
       The name of the new variable(s).

   -Value Object
       The value to assign to the variable, may be piped.

   -Scope string
       The scope in which this variable is valid. 
       Valid values are "Global", "Local", "Private" or "Script", or a number 
       relative to the current scope ( 0 through the number of scopes, where 
       0 is the current scope and 1 is its parent). "Local" is the default.
       For more, type "get-help about_scope".

   -Description string
       A description of the variable

   -Option string
       Where the new variable should be visible/changeable:

       Valid values are:

         None      Set no options. ("None" is the default.)

         ReadOnly  The value of the variable cannot be changed except by using
                   the Force parameter. You can use Remove-Variable to delete the variable.

         Constant  The variable cannot be deleted, and its properties cannot be changed.
                   "Constant" is available only when you are creating an alias.
                   You cannot change the option of an existing variable to "Constant".

         Private   The variable is available only within the scope specified by the Scope parameter.
                   It is inherited by child scopes. (This value is not related to the
                   "Private" value of the Visibility parameter.)

         AllScope  The variable is copied to any new scopes that are created.

       To see the Options property of the variables, type:
          "get-variable| format-table -property name, options -autosize"

   -Force 
       Override restrictions as long as security is not compromised.
       Allow a new variable to be created with the same name as an existing read-only variable.

   -Visibility {Public | Private}
       Whether the variable is visible outside of the session in which it was created.
       This parameter is designed for  use in scripts and commands that will be delivered to other users.

       When a variable is private, it does not appear in lists of variables, such as
       those returned by Get-Variable, or in displays of the Variable: drive.
       Commands to read or change the value of a private variable return an error.
       However, the user can run commands that use a private variable if the commands were
       written in the session in which the variable was defined.

   -PassThru
       Pass the object through the pipeline. By default, New-Variable 
       does not pass any objects through the pipeline.
        
   -WhatIf
       Describe what would happen if you executed the command without
       actually executing the command.
        
   -Confirm
       Prompt for confirmation before executing the command.

Standard Aliases for New-Variable: nv

A private variable will be visible only to the current function, by default variables are visible to the current script, or if typed on the command line, to the current session/scope.

A ReadOnly variable can still be changed (or removed) by specifying -force.

A constant variable cannot be changed or removed, it is constant for the duration of the session.

The AllScope option will automatically copy the variable to any new child scope.

Examples

Create a new variable:

PS C:\> new-variable delivery_day
PS C:\> set-variable delivery_day 'Monday'
PS C:\> "$delivery_day"

Create a new variable and assign a value:

PS C:\> new-variable zipcode -value 90210
PS C:\> "$zipcode"
PS C:\> $zipcode=54398
PS C:\> "$zipcode"

Create a new private variable:

PS C:\> new-variable -name SS64 -visibility private

“Everything is on the move, nothing is constant” ~ Heraclitus

Related PowerShell Cmdlets

Clear-Variable - Remove the value from a variable.
Get-Variable - Get a PowerShell variable.
New-Variable - Create a new variable.
Remove-Variable - Remove a variable and its value.
Set-Variable - Set a variable.
How-to: Variables and Operators - Create variable, add to value, subtract, divide.
Equivalent bash command: export - Set an environment variable.


 
Copyright © 1999-2024 SS64.com
Some rights reserved