PowerShell Logo Small


This is the built-in help made by Microsoft for the command 'Set-Variable', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' 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.


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


Set-Variable [-Name] <String[]> [[-Value] [<Object>]] [-Description [<String>]] [-Exclude [<String[]>]] [-Force] [-Include [<String[]>]] [-InformationAction
{SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]] [-Option {None | ReadOnly | Constant | Private | AllScope |
Unspecified}] [-PassThru] [-Scope [<String>]] [-Visibility {Public | Private}] [-Confirm] [-WhatIf] [<CommonParameters>]

Search powershellhelp.space


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.



Online Version: http://go.microsoft.com/fwlink/p/?linkid=294015




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

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

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

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

PS C:\>set-variable -name counter -visibility private
PS C:\>get-variable c*

Name Value
---- -----
Culture en-US
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.