about_Variables - PowerShell command help and examples

Describes how variables store values that can be used in Windows (about_Variables)

TOPIC
about_Variables
SHORT DESCRIPTION
Describes how variables store values that can be used in Windows PowerShell.
LONG DESCRIPTION
A variable is a unit of memory in which values are stored. In Windows PowerShell, variables are represented by single-word text strings that begin with the dollar sign ($), such as $a, $process, or $my_var. There are several different types of variables in Windows PowerShell. -- User-created variables: User-created variables are created and maintained by the user. By default, the variables that you create at the Windows PowerShell command line exist only while the Windows PowerShell window is open, and they are lost when you close the window. To save a variable, add it to your Windows PowerShell profile. You can also create variables in scripts with global, script, or local scope. -- Automatic variables: Automatic variables store the state of Windows PowerShell. These variables are created by Windows PowerShell, and Windows PowerShell changes their values as required to maintain their accuracy. Users cannot change the value of these variables. For example, the $PSHome variable stores the path to the Windows PowerShell installation directory. For more information, a list, and a description of the automatic variables, see about_Automatic_Variables. -- Preference variables: Preference variables store user preferences for Windows PowerShell. These variables are created by Windows PowerShell and are populated with default values. Users can change the values of these variables. For example, MaximumHistoryCount determines the maximum number of entries in the session history. For more information, a list, and a description of the preference variables, see about_Preference_Variables. WORKING WITH VARIABLES To list all of the variables in your Windows PowerShell session, type: get-variable To display the value of any variable, type the name of the variable, preceded by a dollar sign ($). Windows PowerShell responds by displaying its value. $<variable-name> For example: PS> $pshome C:\Windows\System32\WindowsPowerShell\v1.0 To create a new variable or to change the value of a variable, use an assignment statement in the following format: $<variable> = <value> For example: PS> $my-variable = 1, 2, 3 or PS> $VerbosePreference = "Continue" To get an object that represents the variable, use a Get-Variable command, such as: PS> get-variable pid To use a variable, type the variable name, including the dollar sign ($), in a command or expression. If the command or expression is not enclosed in quotation marks or if it is enclosed in double-quotation marks ("), the value of the variable is used in the command or expression. If the command is enclosed in single quotation marks, ('), the variable name is used in the expression. For example, the first command finds the value of the $profile variable, which is the path to the Windows PowerShell user profile file. The second command opens the file in Notepad. PS> $profile C:\Documents and Settings\User01\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 PS> Notepad $profile You can store any type of object in a variable, including integers, strings, arrays, and hash tables, objects that represent processes, services, event logs, and computers. SAVING VARIABLES Variables that you create are available only in the session in which you create them. They are lost when you close your session. To save a variable, add the variable to your Windows PowerShell profile. Variables in a profile are added to every Windows PowerShell session that you open. For example, to change the value of the $VerbosePreference variable in every Windows PowerShell session, add the following command to your Windows PowerShell profile. $VerbosePreference = "Continue" You can add this command to your profile by opening the profile file in a text editor, such as Notepad, or you can use an Add-Content command, like the following one. The following command adds the new value for the $VerbosePreference variable to the CurrentUser,AllHosts profile. add-content -path $profile.CurrentUserAllHosts -value '$VerbosePreference = "Continue"' For more information about Windows PowerShell profiles, see about_profiles. VARIABLE NAMES WITH SPECIAL CHARACTERS You can use braces to force Windows PowerShell to interpret a variable name literally. This is especially helpful when creating or referring to a variable name that includes special characters, such as dashes, periods, colons, and parentheses. To create a variable name that includes a hyphen, enclose the variable name in braces. The following command creates a variable named "save-items". C:\PS> ${save-items} = "a", "b", "c" C:\PS>${save-items} a b c To refer to a variable name that includes parentheses, enclose the variable name in braces. For example, the following command gets the child items in the directory stores in the "ProgramFiles(x86)" environment variable. C:\PS> Get-childitem ${env:ProgramFiles(x86)} To refer to a variable name that includes braces, enclose the variable name in braces, and use the backtick (escape) character to escape the braces. For example, to create a variable named "this{value}is" with a value of 1, type: C:\PS> ${this`{value`}is} = 1 C:\PS> ${this`{value`}is} 1 THE VARIABLE: DRIVE Windows PowerShell includes a Variable: drive that looks and acts like a file system drive, but it contains the variables in your session. To change to the variable drive, type: set-location variable: (or "cd variable:") When in the Variable drive, to list the items (variables) in the drive, use the Get-ChildItem cmdlet. For example: get-childitem (or "dir" or "ls") For more information about the Variable: drive and the Windows PowerShell Variable provider, type: get-help variable SEE ALSO about_Automatic_Variables about_Environment_Variables about_Preference_Variables about_Scopes C:\Windows>powershell get-help about_While -full

Microsoft Windows [Version 10.0.19045.3693]
Copyright (c) 2023 Microsoft Corporation.

ColorConsole [Version 3.7.1000] PowerShell 2.0-Export

Windows 11, 10, 8.1, 8, 7 / Server 2022, 2019, 2016











Windows-10


... Windows 10 FAQ
... Windows 10 How To


Windows 10 How To


... Windows 11 How To
... Windows 10 FAQ



PowerShell: Describes how variables store values that can be used in Windows

HTTP: ... PS_Windows/en/about_Variables.htm
0.062
16023
Erstellen eines Screenshots mit Paint in Windows 10/11?
Wie kann ich den SHA Hashwert einer Datei mit PowerShell herausfinden?
Das Laufwerk sicher entfernen ist in der falschen Sprache, geht es auch in Deutsch?
Write short invitations and reports on Windows 10/11!
Desktop Hintergrund bei nicht aktiviertem Windows 11 / 10 ändern?
Drucker Ausgabefach einstellen unter Windows 11, 10, ... Desktop und Server!
Wo finde und starte ich das PowerShell in Windows 11 und 10?
Windows 10 / 11 and computer management!
Quick Rotate / Flip Desktop Screen via Keyboard Shortcut on Windows 10 and 8.1!
Should I install 32 bit or 64 bit Windows 11/10, x64 vs x32!



(0)