New-Variable
Creates a new variable.
New-Variable [-Name*] <String> [[-Value] [<Object>]] [-Description [<String>]] [-Force] [-Option {None | ReadOnly |Constant | Private | AllScope | Unspecified}] [-PassThru] [-Scope [<String>]] [-Visibility {Public | Private}][-Confirm] [-WhatIf] [<CommonParameters>]
The New-Variable cmdlet creates a new variable in Windows PowerShell. You can assign a value to the variable while creating it or assign or change the value after it is created.
You can use the parameters of New-Variable to set the properties of the variable (such as those that create read-only or constant variables), set the scope of a variable, and determine whether variables are public or private.
Typically, you create a new variable by typing the variable name and its value, such as $Var = 3, but you can use the New-Variable cmdlet to use its parameters.
Parameters |
---|
-Description [<String>]
|
-Force [<SwitchParameter>]
|
-Name <String>
|
-Option [<ScopedItemOptions>]
|
-PassThru [<SwitchParameter>]
|
-Scope [<String>]
|
-Value [<Object>]
|
-Visibility [<SessionStateEntryVisibility>]
|
-Confirm [<SwitchParameter>]
|
-WhatIf [<SwitchParameter>]
|
<CommonParameters>
|
Inputs
System.Object
You can pipe a value to New-Variable.
Outputs
None or System.Management.Automation.PSVariable
When you use the PassThru parameter, New-Variable generates a System.Management.Automation.PSVariable object representing the new variable. Otherwise, this cmdlet does not generate any output.
Examples
- Create a variable:
PS C:> New-Variable days
This command creates a new variable named days. You are not required to type the Name parameter.
- Create a variable and assign it a value:
PS C:> New-Variable -Name zipcode -Value 98033
This command creates a variable named zipcode and assigns it the value 98033.
- Create a variable with the ReadOnly option:
PS C:> New-Variable -Name Max -Value 256 -Option ReadOnly PS C:> New-Variable -Name max -Value 1024 New-Variable : A variable with name 'max' already exists. At line:1 char:13 + new-variable PS C:> New-Variable -Name max -Value 1024 -Force
This example shows how to use the ReadOnly option of New-Variable to protect a variable from being overwritten.
The first command creates a new variable named Max and sets its value to 256. It uses the Option parameter with a value of ReadOnly.
The second command tries to create a second variable with the same name. This command returns an error, because the read-only option is set on the variable.
The third command uses the Force parameter to override the read-only protection on the variable. In this case, the command to create a new variable with the same name succeeds.
- Create a private variable:
PS C:> New-Variable -Name counter -Visibility private #Effect of private variable in a module. PS C:> Get-Variable c* Name Value ---- ----- Culture en-US ConsoleFileName ConfirmPreference High CommandLineParameters {}PS C:>$counter "Cannot access the variable '$counter' because it is a private variable"PS C:>Get-Counter Name Value ---- ----- Counter1 3.1415 ...
This command demonstrates the behavior of a private variable in a module. The module contains the Get-Counter cmdlet, which has a private variable named Counter. The command uses the Visibility parameter with a value of Private to create the variable.
The sample output shows the behavior of a private variable. The user who has loaded the module cannot view or change the value of the Counter variable, but the Counter variable can be read and changed by the commands in the module.
Related Links