Variables
This page will have a detailed look on variables.
The first thing you need to know: what is a variable? In your script it looks like a word that begins with a $ ($myVar).
But inside the pc, you say to your memory "hey give me some space (reserve some memory) to store my data in.
Sometimes you see people talking about integers, strings, booleans, etc... what is this? Well when you reserve some memory it would be nice if the
memory knew how much (there are other things to but we will go not further than this). Let's say we want to store a byte in $myByte. Then the memory
knows it should reserve 1 byte of space to store the byte. It's simple to write it in code: [byte]$myByte
Variables should always start with a $. These characters are illegal to use in a variable name: ! @ # % & , . and space.
Variable names are not case sensitive.
Here's a list of some available types in PowerShell:
- [int] - 32-bit signed integer
- [long] - 64-bit signed integer
- [string] - Fixed-length string of Unicode characters
- [char] - A Unicode 16-bit character
- [byte] - An 8-bit unsigned character
- [bool] - Boolean True/False value
- [decimal] - An 128-bit decimal value
- [single] - Single-precision 32-bit floating point number
- [double] - Double-precision 64-bit floating point number
- [xml] - Xml object
- [array] - An array of values
- [hashtable] - Hashtable object
When choosing a variable name you can come up with almost everything but you cannot use these reserved keywords:
- break
- continue
- do
- else
- elseif
- filter
- foreach
- function
- if
- in
- return
- switch
- until
- where
- while
In PowerShell there is also a list of variables that you cannot use because PowerShell uses them.
To get the full list with all info type in Get-Help about_automatic_variables.
- $$ - contains the last token of last line input into the shell
- $? - Contains that success/fail status of the last operation
- $^ - contains the first token of the last line input into the shell
- $DebugPreference - The action to take when data is written via write-debug in a script or WriteDebug in a cmdlet or provider.
- $ErrorActionPreference - The action to take when data is written via write-error in a script or WriteError in a cmdlet or provider.
- $HistorySize - Number of entries saved in the command history.
- $PSCommandPath - The paths where .cmdlet and .cmdletprovider files may be found. This is the Windows PowerShell equivalent of the CMD.EXE $PATH.
- $ReportErrorShowExceptionClass - Set to true indicates that the class name of the exception(s) displayed will be shown. Default at internal startup is false.
- $ReportErrorShowInnerException - Set to true indicates that the chain of inner exceptions should be shown. Each exception message will be indented from the previous message. The display of each exception is governed by the same options as the root exception, meaning that the options dictated by $ReportShowError* will be used to display each exception. Default is false.
- $ReportErrorShowSource - Set to true indicates that the assembly name from whence the exception originated will be displayed. Default at internal startup is true.
- $ReportErrorShowStackTrace - Set to true indicates that the stack trace of the exception will be emitted. Default at internal startup is false.
- $ShouldProcessPolicy - The action to take when ShouldProcess is used in a cmdlet.
- $ShouldProcessReturnPolicy - ShouldProcess will return this setting
- $StackTrace - The current execution stacktrace - currently, this may be overwritten.
- $VerbosePreference - The action to take when data is written via write-verbose in a script or WriteVerbose in a cmdlet or provider.
- $_ - The current pipeline object, used in script blocks and where
- $Args - Used in creating functions that require parameters
- $Error - Objects which had an error occur while processing that object in a cmdlet.
- $foreach - Reference to the enumerator in a foreach loop
- $Home - The users home directory; set to %HOMEDRIVE%\%HOMEPATH%
- $Input - Can aid in code blocks that are in the middle of a pipeline, (see code block)
- $PSHome - The install location of Windows PowerShell
- $Host - Information about the current executing host
- $OFS - Output Field Separator
- $StackTrace - contains detailed stack trace information about the last error
This page will be updated soon...