Compartir a través de


Set-Variable

Sets the value of a variable. Creates the variable if one with the requested name does not exist.

Sintaxis

Default (Es el valor predeterminado).

Set-Variable
    [-Name] <String[]>
    [[-Value] <Object>]
    [-Include <String[]>]
    [-Exclude <String[]>]
    [-Description <String>]
    [-Option <ScopedItemOptions>]
    [-Force]
    [-Visibility <SessionStateEntryVisibility>]
    [-PassThru]
    [-Scope <String>]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

Description

The Set-Variable cmdlet assigns a value to a specified variable or changes the current value. If the variable does not exist, the cmdlet creates it.

Ejemplos

Example 1: Set a variable and get its value

These commands set the value of the $desc variable to A description, and then gets the value of the variable.

Set-Variable -Name "desc" -Value "A description"
Get-Variable -Name "desc"
Name                           Value
----                           -----
desc                           A description

Example 2: Set a global, read-only variable

This example creates a global, read-only variable that contains all processes on the system, and then it displays all properties of the variable.

Set-Variable -Name "processes" -Value (Get-Process) -Option Constant -Scope Global -Description "All processes" -PassThru |
    Format-List -Property *

The command uses the Set-Variable cmdlet to create the variable. It uses the PassThru parameter to create an object representing the new variable, and it uses the pipeline operator (|) to pass the object to the Format-List cmdlet. It uses the Property parameter of Format-List with a value of all (*) to display all properties of the newly created variable.

The value, (Get-Process), is enclosed in parentheses to ensure that it is executed before being stored in the variable. Otherwise, the variable contains the words Get-Process.

Example 3: Understand public vs. private variables

This example shows how to change the visibility of a variable to Private. This variable can be read and changed by scripts with the required permissions, but it is not visible to the user.

New-Variable -Name "counter" -Visibility Public -Value 26
$Counter
26
Get-Variable c*
Name                  Value
----                  -----
Culture               en-US
ConsoleFileName
ConfirmPreference     High
CommandLineParameters {}
Counter               26
Set-Variable -Name "counter" -Visibility Private
Get-Variable c*
Name                  Value
----                  -----
Culture               en-US
ConsoleFileName
ConfirmPreference     High
CommandLineParameters {}
$counter
"Cannot access the variable '$counter' because it is a private variable"
.\use-counter.ps1
#Commands completed successfully.

This command shows how to change the visibility of a variable to Private. This variable can be read and changed by scripts with the required permissions, but it is not visible to the user.

Parámetros

-Confirm

Prompts you for confirmation before running the cmdlet.

Propiedades del parámetro

Tipo:SwitchParameter
Valor predeterminado:False
Admite caracteres comodín:False
DontShow:False
Alias:cf

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-Description

Specifies the description of the variable.

Propiedades del parámetro

Tipo:String
Valor predeterminado:None
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-Exclude

Specifies an array of items that this cmdlet excludes from the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcards are permitted.

Propiedades del parámetro

Tipo:

String[]

Valor predeterminado:None
Admite caracteres comodín:True
DontShow:False

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-Force

Allows you to create a variable with the same name as an existing read-only variable, or to change the value of a 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.

Propiedades del parámetro

Tipo:SwitchParameter
Valor predeterminado:False
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-Include

Specifies an array of items that this cmdlet includes in the operation. The value of this parameter qualifies the Name parameter. Enter a name or name pattern, such as c*. Wildcards are permitted.

Propiedades del parámetro

Tipo:

String[]

Valor predeterminado:None
Admite caracteres comodín:True
DontShow:False

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-Name

Specifies the variable name.

Propiedades del parámetro

Tipo:

String[]

Valor predeterminado:None
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

(All)
Posición:0
Mandatory:True
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:True
Valor de los argumentos restantes:False

-Option

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

These values are defined as a flag-based enumeration. You can combine multiple values together to set multiple flags using this parameter. The values can be passed to the Option parameter as an array of values or as a comma-separated string of those values. The cmdlet will combine the values using a binary-OR operation. Passing values as an array is the simplest option and also allows you to use tab-completion on the values.

Propiedades del parámetro

Tipo:ScopedItemOptions
Valor predeterminado:None
Valores aceptados:None, ReadOnly, Constant, Private, AllScope, Unspecified
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-PassThru

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

Propiedades del parámetro

Tipo:SwitchParameter
Valor predeterminado:False
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-Scope

Specifies the scope of the variable.The acceptable values for this parameter are:

  • Global
  • Local
  • Script
  • Private
  • 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.

Propiedades del parámetro

Tipo:String
Valor predeterminado:Local
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-Value

Specifies the value of the variable.

Propiedades del parámetro

Tipo:Object
Valor predeterminado:None
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

(All)
Posición:1
Mandatory:False
Valor de la canalización:True
Valor de la canalización por nombre de propiedad:True
Valor de los argumentos restantes:False

-Visibility

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.

Propiedades del parámetro

Tipo:SessionStateEntryVisibility
Valor predeterminado:Public
Valores aceptados:Public, Private
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-WhatIf

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

Propiedades del parámetro

Tipo:SwitchParameter
Valor predeterminado:False
Admite caracteres comodín:False
DontShow:False
Alias:wi

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Entradas

Object

You can pipe an object representing the value of the variable to this cmdlet.

Salidas

None

By default, this cmdlet returns no output.

PSVariable

When you use the PassThru parameter, this cmdlet returns a PSVariable object representing the new or changed variable.

Notas

Windows PowerShell includes the following aliases for Set-Variable:

  • set
  • sv