Contact Info

Crumbtrail » Administration » Powershell » Powershell 1.0 » Set-Variable

Set-Variable - Powershell 1.0 CmdLet

ActiveXperts Network Monitor ships with integrated Powershell scripts to monitor complex network. The scripts run out of the box
Download the ActiveXperts Network Monitor FREE version now »


Set a variable and a value

Set-Variable [-Name] String[] [-value] Object] 
           [-Include string[]] [-Exclude string[]] [-option option]
              [-scope string] [-force] [-passThru] [-whatIf]
                 [-confirm][-description string] [CommonParameters]

-Name String
       The name of the variable(s), may be piped.

   -value Object
       The value to assign to the variable.
   -include string
       Items upon which Set-variable will act, excluding all others.

   -exclude string
       Items upon which Set-variable will not act, include all others.

   -option option
       The valid options are: 
         None    : Set no options. (default) 
         ReadOnly: The alias cannot be changed unless you use -Force. 
         Constant: The alias cannot be changed, even by using -Force. 
         Private : The alias is available only within the scope specified by -Scope. 
                   It is invisible in all other scopes.  
   -scope string
       The scope in which this alias is valid. 
       Valid values are "Global", "Local", "Private" 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, type "get-help about_scope".

       Override restrictions that prevent the command from succeeding, apart
       from security settings. Make the best attempt at setting the variable.

       Pass the object created by this cmdlet through the pipeline.

       Describe what would happen if you executed the command without actually
       executing the command.

       Prompt for confirmation before executing the command.

   -description string
       The description of the variable.
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable.

Set the value of the "SS64" variable to "My Sample text ":

PS C:\>set-variable -name SS64 -value "My Sample text"

Create a global, read-only variable that contains all processes on the system:

PS C:\>set-variable -name myprocs -value (Get-Process) -option constant -scope global

In the example above, the value is enclosed in parentheses this executes the command: (Get-Process) before storing the result in the variable, rather than just storing the text "Get-Process".