Export (0) Print
Expand All
Expand Minimize

New-Variable

Updated: December 3, 2014

Applies To: Windows PowerShell 4.0

New-Variable

Creates a new variable.

Aliases

The following abbreviations are aliases for this cmdlet:

  • nv

Syntax

Parameter Set: Default
New-Variable [-Name] <String> [[-Value] <Object> ] [-Description <String> ] [-Force] [-Option <ScopedItemOptions> ] [-PassThru] [-Scope <String> ] [-Visibility <SessionStateEntryVisibility> ] [-Confirm] [-WhatIf] [ <CommonParameters>]




Detailed 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 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>

Specifies a description of the variable.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Force

Allows you to create a new variable with the same name as an existing read-only variable.

By default, you can overwrite a variable unless the variable has an option value of ReadOnly or Constant. For more information, see the Option parameter.


Aliases

none

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Name<String>

Specifies a name for the new variable.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Option<ScopedItemOptions>

Sets the value of the Options property of the variable.

Valid values are:

-- None: Sets no options. ("None" is the default.)

-- ReadOnly: Can be deleted. Cannot be not changed, except by using the Force parameter.

-- Constant: Cannot be deleted or changed. "Constant" is valid only when you are creating a variable. You cannot change the options of an existing variable to "Constant".

-- Private: The variable is available only in the current scope.

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

To see the Options property of all variables in the session, type "get-variable | format-table -property name, options -autosize".


Aliases

none

Required?

false

Position?

named

Default Value

"None"

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-PassThru

Returns an object representing the new variable. By default, this cmdlet does not generate any output.


Aliases

none

Required?

false

Position?

named

Default Value

No output

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Scope<String>

Determines the scope of the new variable. Valid values are "Global", "Local", 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 information, see about_Scopes.


Aliases

none

Required?

false

Position?

named

Default Value

Local

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Value<Object>

Specifies the initial value of the variable.


Aliases

none

Required?

false

Position?

2

Default Value

none

Accept Pipeline Input?

true (ByValue, ByPropertyName)

Accept Wildcard Characters?

false

-Visibility<SessionStateEntryVisibility>

Determines 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.

Valid values are:

-- Public: The variable is visible. ("Public" is the default.)

-- Private: The variable is not visible.

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.


Aliases

none

Required?

false

Position?

named

Default Value

Public

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Confirm

Prompts you for confirmation before running the cmdlet.


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see  about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • System.Object

    You can pipe a value to New-Variable.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • 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

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

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


PS C:\> new-variable days

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

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


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

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

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.


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 <<<<  -name max -value 1024

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

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

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.


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
...

Related topics



Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft