<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://vwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Category%3AVariables_%28PowerShell%29</id>
	<title>Category:Variables (PowerShell) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://vwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Category%3AVariables_%28PowerShell%29"/>
	<link rel="alternate" type="text/html" href="http://vwiki.co.uk/index.php?title=Category:Variables_(PowerShell)&amp;action=history"/>
	<updated>2026-05-20T08:32:15Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>http://vwiki.co.uk/index.php?title=Category:Variables_(PowerShell)&amp;diff=2323&amp;oldid=prev</id>
		<title>Sstrutt: Initial creation - content from Variables page</title>
		<link rel="alternate" type="text/html" href="http://vwiki.co.uk/index.php?title=Category:Variables_(PowerShell)&amp;diff=2323&amp;oldid=prev"/>
		<updated>2013-06-21T14:39:29Z</updated>

		<summary type="html">&lt;p&gt;Initial creation - content from Variables page&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;All variable names are prefixed with a &amp;lt;code&amp;gt;$&amp;lt;/code&amp;gt;, and are case &amp;#039;&amp;#039;&amp;#039;insensitive&amp;#039;&amp;#039;&amp;#039; (though there&amp;#039;s no reason to not use CamelBack notation if that floats your boat).&lt;br /&gt;
&lt;br /&gt;
Apart from a few reserved words and characters there&amp;#039;s little restriction on what can be used, though note that this flexibility can cause the occasional issue, whereby PowerShell gets confused as to where a variable name finishes. Variable names can be enclosed in &amp;lt;code&amp;gt; { } &amp;lt;/code&amp;gt; in order to delimit them, eg &amp;lt;code&amp;gt;${varname}&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;PowerShell is all about manipulating objects, and all variables are objects.&amp;#039;&amp;#039;&amp;#039;  When you assign a value to a new variable, the type of object that it is (string, integer, etc) is automatically defined. Which is normally useful as you don&amp;#039;t have to worry about what type of object/variable you want to create, PowerShell will work it out for you.  However sometimes you need to force a variable to contain a specific data type to avoid errors or other problems down the line. Using a prefix of &amp;lt;code&amp;gt;[type]&amp;lt;/code&amp;gt; achieves this...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt; [string]$result = $PingResult.Status &amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Data types ==&lt;br /&gt;
{|class=&amp;quot;vwikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Notation                      !! Data Type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [adsi] &amp;lt;/code&amp;gt; 	|| Active Directory Services object&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [array] &amp;lt;/code&amp;gt; 	|| Array (see &amp;#039;&amp;#039;&amp;#039;[[Arrays_(PowerShell)|Arrays]]&amp;#039;&amp;#039;&amp;#039;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [bool] &amp;lt;/code&amp;gt;   	|| True / false (can also be &amp;lt;code&amp;gt;[boolean]&amp;lt;/code&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [byte] &amp;lt;/code&amp;gt; 	|| 8-bit unsigned character&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [char]  &amp;lt;/code&amp;gt;	|| Single character&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [datetime] &amp;lt;/code&amp;gt;     || Date and time (see &amp;#039;&amp;#039;&amp;#039;[[Object_Types_(PowerShell)#Datetime|Datetime]]&amp;#039;&amp;#039;&amp;#039;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [decimal] &amp;lt;/code&amp;gt; 	|| 128-bit decimal&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [double] &amp;lt;/code&amp;gt; 	|| Double-precision 64-bit floating point number&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [int] &amp;lt;/code&amp;gt; 		|| 32-bit integer&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [long] &amp;lt;/code&amp;gt; 	|| 64-bit integer&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [single] &amp;lt;/code&amp;gt; 	|| Single-precision 32-bit floating point number&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [string] &amp;lt;/code&amp;gt; 	|| String of characters (see &amp;#039;&amp;#039;&amp;#039;[[Strings_(PowerShell)|Strings]]&amp;#039;&amp;#039;&amp;#039;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [timespan] &amp;lt;/code&amp;gt;     || Duration of time&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [wmi] &amp;lt;/code&amp;gt; 		|| Windows Management Instrumentation (WMI) instance or collection&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [wmiclass] &amp;lt;/code&amp;gt; 	|| WMI class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; [xml] &amp;lt;/code&amp;gt; 		|| XML object&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Variable Information ==&lt;br /&gt;
As PowerShell object (variables) tend to be black boxes that can contain anything or nothing, its often necessary to understand more about one.  All objects contain Properties and Methods.&lt;br /&gt;
* Properties&lt;br /&gt;
** Are containers for data&lt;br /&gt;
* Methods&lt;br /&gt;
** Are in-built functions that allow an object to be manipulated.&lt;br /&gt;
&lt;br /&gt;
=== Variable Type ===&lt;br /&gt;
To see the object type of a variable...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
PS E:\&amp;gt; $var.GetType()&lt;br /&gt;
IsPublic IsSerial Name                                     BaseType&lt;br /&gt;
-------- -------- ----                                     --------&lt;br /&gt;
True     True     Object[]                                 System.Array&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Variable Properties ===&lt;br /&gt;
Simple objects such as strings will contain only one property but complicated can contain many more, and even further objects.  If you pipe an object through &amp;lt;code&amp;gt;Format-List&amp;lt;/code&amp;gt; you get a fuller picture...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
PS E:\&amp;gt; Get-WMIObject Win32_BIOS&lt;br /&gt;
&lt;br /&gt;
SMBIOSBIOSVersion : 786F3 v01.16&lt;br /&gt;
Manufacturer      : Hewlett-Packard&lt;br /&gt;
Name              : Default System BIOS&lt;br /&gt;
SerialNumber      : CPC123456J&lt;br /&gt;
Version           : HPQOEM - 20090219&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
PS E:\&amp;gt; Get-WMIObject Win32_BIOS | Format-List *&lt;br /&gt;
&lt;br /&gt;
Status                : OK&lt;br /&gt;
Name                  : Default System BIOS&lt;br /&gt;
Caption               : Default System BIOS&lt;br /&gt;
SMBIOSPresent         : True&lt;br /&gt;
....TRUNCATED TO SAVE SPACE !!....&lt;br /&gt;
SMBIOSBIOSVersion     : 786F3 v01.16&lt;br /&gt;
SMBIOSMajorVersion    : 2&lt;br /&gt;
SMBIOSMinorVersion    : 6&lt;br /&gt;
SoftwareElementID     : Default System BIOS&lt;br /&gt;
SoftwareElementState  : 3&lt;br /&gt;
TargetOperatingSystem : 0&lt;br /&gt;
Version               : HPQOEM - 20090219&lt;br /&gt;
Scope                 : System.Management.ManagementScope&lt;br /&gt;
Path                  : \\LAPTOP1\root\cimv2:Win32_BIOS.Name=&amp;quot;Default System BIOS&amp;quot;,SoftwareElementID=&amp;quot;Default System BI&lt;br /&gt;
                        OS&amp;quot;,SoftwareElementState=3,TargetOperatingSystem=0,Version=&amp;quot;HPQOEM - 20090219&amp;quot;&lt;br /&gt;
Options               : System.Management.ObjectGetOptions&lt;br /&gt;
ClassPath             : \\LAPTOP1\root\cimv2:Win32_BIOS&lt;br /&gt;
Properties            : {BiosCharacteristics, BIOSVersion, BuildNumber, Caption...}&lt;br /&gt;
SystemProperties      : {__GENUS, __CLASS, __SUPERCLASS, __DYNASTY...}&lt;br /&gt;
Qualifiers            : {dynamic, Locale, provider, UUID}&lt;br /&gt;
Site                  :&lt;br /&gt;
Container             :&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Variable Properties and Methods ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
PS E:\ &amp;gt; $string | get-Member&lt;br /&gt;
&lt;br /&gt;
   TypeName: System.String&lt;br /&gt;
&lt;br /&gt;
Name             MemberType            Definition&lt;br /&gt;
----             ----------            ----------&lt;br /&gt;
Clone            Method                System.Object Clone()&lt;br /&gt;
CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB)&lt;br /&gt;
Contains         Method                bool Contains(string value)&lt;br /&gt;
CopyTo           Method                System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex,...&lt;br /&gt;
EndsWith         Method                bool EndsWith(string value), bool EndsWith(string value, System.StringCompari...&lt;br /&gt;
Equals           Method                bool Equals(System.Object obj), bool Equals(string value), bool Equals(string...&lt;br /&gt;
GetEnumerator    Method                System.CharEnumerator GetEnumerator()&lt;br /&gt;
....TRUNCATED TO SAVE SPACE !!....&lt;br /&gt;
ToString         Method                string ToString(), string ToString(System.IFormatProvider provider)&lt;br /&gt;
ToUpper          Method                string ToUpper(), string ToUpper(System.Globalization.CultureInfo culture)&lt;br /&gt;
ToUpperInvariant Method                string ToUpperInvariant()&lt;br /&gt;
Trim             Method                string Trim(Params char[] trimChars), string Trim()&lt;br /&gt;
TrimEnd          Method                string TrimEnd(Params char[] trimChars)&lt;br /&gt;
TrimStart        Method                string TrimStart(Params char[] trimChars)&lt;br /&gt;
Chars            ParameterizedProperty char Chars(int index) {get;}&lt;br /&gt;
Length           Property              System.Int32 Length {get;}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It&amp;#039;s not uncommon for objects provided by API&amp;#039;s to provide methods that are in fact objects themselves, you can end up with a lot of information being available once you&amp;#039;re capable of drilling into them.&lt;br /&gt;
&lt;br /&gt;
=== NULL ===&lt;br /&gt;
Checking whether a variable is NULL or not can be problematic.  In that the easiest test doesn&amp;#039;t always work.&lt;br /&gt;
&lt;br /&gt;
Basic test for whether a variable exists or not, works in most cases...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
if (!$var) {&lt;br /&gt;
    &amp;quot;Variable is null&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you expect your variable to be NULL or contain a positive number...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
if ($var -gt 0) {&lt;br /&gt;
    &amp;quot;Variable is NOT null&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Last resort test...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
if ($var.gettype().ToString() -eq &amp;quot;System.DBNull&amp;quot;) {&lt;br /&gt;
    &amp;quot;Variable is null&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Scope ==&lt;br /&gt;
Variables only fully exist within the scope of the script or function within which they are defined.  Within functions local copies of a variable are available, but manipulating them has no effect on the real/master variable in the main scope, to get around this you can use &amp;lt;code&amp;gt;script&amp;lt;/code&amp;gt; to enforce using the variable that&amp;#039;s in the main script&amp;#039;s scope...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function Local-Add($text) {&lt;br /&gt;
    $List += $text&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function Global-Add($text) {&lt;br /&gt;
    $script:List += $text&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$List = @() &lt;br /&gt;
$List += &amp;quot;Text message ONE&amp;quot;&lt;br /&gt;
$List.Length                           # List contains one entry&lt;br /&gt;
Local-Add &amp;quot;Text message TWO&amp;quot;&lt;br /&gt;
$List.Length                           # List still contains one entry&lt;br /&gt;
Global-Add &amp;quot;Text message THREE&amp;quot;&lt;br /&gt;
$List.Length                           # List now contains two entries&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, you can create a variable within a function that has global scope...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function Create-Var {&lt;br /&gt;
    if (!$TestVar) {&lt;br /&gt;
        $script:TestVar = &amp;quot;stuff&amp;quot;&lt;br /&gt;
        Write-Host &amp;#039;Created $TestVar&amp;#039;&lt;br /&gt;
    } else {&lt;br /&gt;
        Write-Host &amp;#039;$TestVar already exists&amp;#039;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Create-Var&lt;br /&gt;
Create-Var&lt;br /&gt;
Write-Host &amp;#039;$TestVar contains [&amp;#039; $TestVar &amp;#039;]&amp;#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
...which when run produces the following output...&lt;br /&gt;
 Created $TestVar&lt;br /&gt;
 $TestVar already exists&lt;br /&gt;
 $TestVar contains [ stuff ]&lt;br /&gt;
&lt;br /&gt;
You can achieve the same result by using the &amp;lt;code&amp;gt;Set-Variable&amp;lt;/code&amp;gt; CmdLet with the &amp;lt;code&amp;gt;-Scope&amp;lt;/code&amp;gt; parameter instead of &amp;lt;code&amp;gt;script&amp;lt;/code&amp;gt;, for example...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function Create-Var {&lt;br /&gt;
    if (!$TestVar) {&lt;br /&gt;
        #$script:TestVar = &amp;quot;stuff&amp;quot;&lt;br /&gt;
        Set-Variable -Name TestVar -Value &amp;quot;stuff&amp;quot; -Scope 1&lt;br /&gt;
        Write-Host &amp;#039;Created $TestVar&amp;#039;&lt;br /&gt;
    } else {&lt;br /&gt;
        Write-Host &amp;#039;$TestVar already exists&amp;#039;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Create-Var&lt;br /&gt;
Create-Var&lt;br /&gt;
Write-Host &amp;#039;$TestVar contains [&amp;#039; $TestVar &amp;#039;]&amp;#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Macros / Built-in Variables ==&lt;br /&gt;
{|class=&amp;quot;vwikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Variable                !! Description&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; $_ &amp;lt;/code&amp;gt;       || Variable passed through pipeline from previous command&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; $? &amp;lt;/code&amp;gt;       || Success/failure of previous statement - see [[#Basic_Error_Handler|Basic_Error_Handler]]&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; $Error &amp;lt;/code&amp;gt;   || Last error - array of recent errors - see [[#$error|$error]]&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; $LastExitCode &amp;lt;/code&amp;gt; || Exit code of the last natively run application&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; $foreach &amp;lt;/code&amp;gt; || Enumerator in a foreach loop&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; $Host &amp;lt;/code&amp;gt;    || Information about the machine being executed on (can also use &amp;lt;code&amp;gt;Get-Host&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; $args &amp;lt;/code&amp;gt;    || Array of arguments passed to a script - see [[#Script_Arguments|Script_Arguments]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Environment Variable ===&lt;br /&gt;
Environmental variables can be accessed via &amp;lt;code&amp;gt; $env &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$env:userprofile                  # User profile (eg C:\Users\joeblogs)&lt;br /&gt;
dir env:                          # Show all available variables&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a full list use...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-ChildItem env:&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
...or for some commonly used ones...&lt;br /&gt;
{|class=&amp;quot;vwikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Variable         !! Description                   !! Example data&lt;br /&gt;
|-&lt;br /&gt;
| APPDATA          || Application data path         || C:\Users\RodHull\AppData\Roaming&lt;br /&gt;
|-&lt;br /&gt;
| COMPUTERNAME     || Local System&amp;#039;s hostname       || LAPTOP-01&lt;br /&gt;
|-&lt;br /&gt;
| LOGONSERVER      || Domain Controller Logged into || DC-SERVER-03&lt;br /&gt;
|-&lt;br /&gt;
| PROCESSOR_ARCHITECTURE     || CPU Architecture    || AMD64&lt;br /&gt;
|-&lt;br /&gt;
| USERDOMAIN       || Logged in user&amp;#039;s domain       || DOMAIN.COM&lt;br /&gt;
|-&lt;br /&gt;
| USERNAME         || Local System&amp;#039;s hostname       || rodhull&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[category:PowerShell]]&lt;/div&gt;</summary>
		<author><name>Sstrutt</name></author>
	</entry>
</feed>