PowerShell Logo Small

New-Variable



This is the built-in help made by Microsoft for the command 'New-Variable', in PowerShell version 2 - as retrieved from Windows version 'Microsoft® Windows Vista™ Ultimate ' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Creates a new variable.

SYNTAX


New-Variable [-Name] <string> [[-Value] <Object>] [-Description <string>] [-Force] [-Option {None | ReadOnly | Constant | Private | AllScope}] [-
PassThru] [-Scope <string>] [-Visibility {Public | Private}] [-Confirm] [-WhatIf] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


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 chang
e 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 us
e its parameters.



<

RELATED LINKS

Online version: http://go.microsoft.com/fwlink/?LinkID=113361
Get-Variable
Set-Variable
Remove-Variable
Clear-Variable

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

C:\PS>new-variable days



Description
-----------
This command creates a new variable named "days". It has no value immediately following the command.








-------------------------- EXAMPLE 2 --------------------------

C:\PS>new-variable zipcode -value 98033



Description
-----------
This command creates a variable named "zipcode" and assigns it the value "98033".








-------------------------- EXAMPLE 3 --------------------------

C:\PS>new-variable -name max -value 256 -option readonly

new-variable -name max -value 1024

new-variable -name max -value 1024 -force

C:\PS> new-variable -name max -value 256 -option readonly

C:\PS> new-variable -name max -value 1024
New-Variable : A variable with name 'max' already exists.
At line:1 char:13
+ new-variable <<<< -name max -value 1024

C:\PS> new-variable -name max -value 1024 -force



Description
-----------
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 th
e 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 variab
le with the same name succeeds.








-------------------------- EXAMPLE 4 --------------------------

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

#Effect of private variable in a module.

C:\PS> get-variable c*

Name Value
---- -----
Culture en-US
ConsoleFileName
ConfirmPreference High
CommandLineParameters {}

C:\PS> $counter
"Cannot access the variable '$counter' because it is a private variable"

C:\PS> Get-Counter
Name Value
---- -----
Counter1 3.1415
...



Description
-----------
This command demonstrates the behavior of a private variable in a module. The module contains the Get-Counter cmdlet, which has a private variabl
e 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 var
iable, but the Counter variable can be read and changed by the commands in the module.