PowerShell Logo Small

Set-Variable



This is the built-in help made by Microsoft for the command 'Set-Variable', in PowerShell version 4 - as retrieved from Windows version 'Microsoft Windows 8.1 Enterprise' 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

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

SYNTAX


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



Search powershellhelp.space

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.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=294015
Clear-Variable
Get-Variable
New-Variable
Remove-Variable

REMARKS

<

Examples


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

PS C:\>set-variable -name desc -value "A description"
PS C:\>get-variable -name desc



These commands set the value of the "desc" variable to "A description", and then get the value of the variable.








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

PS C:\>set-variable -name processes -value (Get-Process) -option constant -scope global -description "All processes" -passthru | format-list -property *



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

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 pip
eline 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 th
e 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 --------------------------

PS C:\># set-variable -name counter -visibility private
PS C:\>new-variable -name counter -visibility public -value 26
PS C:\>$counter
26
PS C:\>get-variable c*

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

PS C:\>set-variable -name counter -visibility private
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:\>.\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.

The sample output shows the difference in the behavior of public and private variables.