Changes

Understanding and Creating Windows PowerShell 1.0 Variables

2,974 bytes added, 21:10, 17 November 2008
Changing the Type of a PowerShell Variable
The process of dynamically changing the type of a variable is sometimes referred to as ''automatic type conversion''.
 
== Pre-defined PowerShell Variables ==
 
In an early section of this chapter the existence of pre-defined Windows PowerShell variables was mentioned (specifically, it is not possible to create a variable using a name assigned to a pre-defined variable). The final area to be covered in this chapter, therefore, is to list some of the more useful pre-defined variables:
 
<table border="1" cellpadding="5" cellspacing="0" id="E3B" style="border-collapse: collapse; border-color:#cccccc; border-style: solid; border-width: 1px; margin-bottom:20px">
<tr bgcolor="#cccccc" style="color:black" valign="top">
<th>
<p>Variable Name</p>
</th>
<th>
<p>Description</p>
</th>
</tr>
 
<tr>
<td>
<p>$true</p>
</td>
<td>
Boolean ''true'' value
</td>
</tr>
 
<tr bgcolor="#e9e9e6">
<td>
$false
</td>
<td>
Boolean ''false'' value
</td>
</tr>
 
<tr>
<td>
<p>$OFS</p>
</td>
<td>
Object collection separator
</td>
</tr>
 
<tr bgcolor="#e9e9e6">
<td>
$home
</td>
<td>
The current user's home directory
</td>
</tr>
 
<tr>
<td>
<p>$pshome</p>
</td>
<td>
The directory into which PowerShell is installed.
</td>
</tr>
 
<tr bgcolor="#e9e9e6">
<td>
$args
</td>
<td>
The parameters passed through to a PowerShell function
</td>
</tr>
 
<tr>
<td>
<p>$Input</p>
</td>
<td>
The current content of the pipeline.
</td>
</tr>
 
<tr bgcolor="#e9e9e6">
<td>
$StackTrace
</td>
<td>
Contains current stack trace information.
</td>
</tr>
 
<tr>
<td>
<p>$host</p>
</td>
<td>
Information about the host in which PowerShell is running.
</td>
</tr>
 
<tr bgcolor="#e9e9e6">
<td>
$lastexitcode
</td>
<td>
The exit code returned by the last executed native Windows executable.
</td>
</tr>
 
<tr>
<td>
<p>$error</p>
</td>
<td>
List of all errors since invocation of PowerShell session (up to maximum defined by $maximumerrorcount).
</td>
</tr>
 
<tr bgcolor="#e9e9e6">
<td>
$maximumerrorcount
</td>
<td>
The maximum number of errors to be recorded during a single PowerShell session. Default is 256.
</td>
</tr>
 
<tr>
<td>
<p>$_</p>
</td>
<td>
The current pipeline object; used in script blocks, filters, functions and loops.
</td>
</tr>
 
<tr bgcolor="#e9e9e6">
<td>
$^
</td>
<td>
The first token of the preceding command line
</td>
</tr>
 
<tr>
<td>
<p>$$</p>
</td>
<td>
The last token of the preceding command line
</td>
</tr>
 
<tr bgcolor="#e9e9e6">
<td>
$foreach
</td>
<td>
The current value of the enumerator of a foreach loop.
</td>
</tr>
 
<tr>
<td>
$?
</td>
<td>
The completion status (true or false) of the preceding command.
</td>
</tr>
 
<tr bgcolor="#e9e9e6">
<td>
$null
</td>
<td>
The NULL object.
</td>
</tr>
 
<tr>
<td>
$MyInvocation
</td>
<td>
Information about the current script line or command. Includes script line number, offset into line, script name, and pipeline position details.
</td>
</tr>
 
<tr bgcolor="#e9e9e6">
<td>
$match
</td>
<td>
Hash table of matches generated by the -match parameter
</td>
</tr>
 
<tr>
<td>
$shellid
</td>
<td>
The identifier of the currently running shell.
</td>
</tr>
</table>