DaveWentzel.com            All Things Data

Variables and Constants

ByRef/ByVal

 

You can call functions by ref or by val.  A by ref example.   When using by value the function may change the value of the variable but when it is done the value of the variable remains unchanged.  The default behavior is basically ByRef.  
Example: Output:
cls            
$MyArray = 10,20,30,40            
$MyCopy = $MyArray            
$MyCopy[1]  = 200            
Echo '$MyArray…'            
$MyArray            
Echo '$MyCopy…'            
$MyCopy            
$MyClone = $MyArray.Clone()            
$MyClone[2] = 300            
Echo '$MyClone…'            
$MyClone            
Echo '$MyCopy…'            
$MyCopy
$MyArray…
10
200
30
40
$MyCopy…
10
200
30
40
$MyClone…
10
200
300
40
$MyCopy…
10
200
30
40
 
$MyArray…
10
200
30
40
$MyCopy…
10
200
30
40
$MyClone…
10
200
300
40
$MyCopy…
10
200
30
40
$MyArray…
10
200
30
40
$MyCopy…
10
200
30
40
$MyClone…
10
200
300
40
$MyCopy…
10
200
30
40

 

Associative Arrays:  named value pair hashtables
 
Constants or Aliases
It's probably more accurate to call these aliases.  These are set automatically by the shell on startup.  This is not a complete list, just the ones I find useful on a regular basis. 
 
Alias
Purpose
$true
TRUE
$false
FALSE
$$
last token received from last line of code
$^
first token received from last line of code
$?
success/fail of last operation
$_
converts the pipeline object.  Used by the where-object pipeline.
$args
array of arguments passed to a function
$Error
array of error objects
$ErrorActionPreference Errors and Error Handling.  Default is Continue (display an error message and keep going).  Stop produces a trappable exception. 
$ErrorView
mode for displaying errors
$HOME
user's home directory
$Profile
location of user's profile
$PWD
print working directory...equivalent of "cd" or "pwd" at a DOS prompt. 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cmdlets for variable manipulation
set-variable
probably only needed in a pipeline
new-variable
probably only needed in a pipeline
get-variable
using -ValueOnly passes only the value, not the object, along the pipeline. 
clear-variable
 
remove-variable
 
 
 
Also, many DOS commands are aliased in PS or are simply exposed to PS directly.  For example, findstr.  dir is also an alias of get-children.  This example shows all aliases on your shell.  Note that aliases are exposed like a drive namespace.  A cmdlet has also been created to do this quickly... get-alias | sort-object Name
 
 
 
 
You can create your own aliases using Set-Alias. 
 
Set-Alias -Name dave -Value Get-Command
 
 
Variables
Variables are manipulated very similarly to drives.  Consider the following:
set-location variable:
get-childitem              note that this shows variables without the $ prepended.  Or you can run:
cd variable:
or from any directory/drive just run:
get-childitem variable:*
 
 
Consider the following example.  We first check that $var does not exist.  Then we set it using a prompt and echo it back to the screen using various methods: 
 
 
 
Scope and Lifetime
Variables and functions created inside a script have the scope and lifetime of the script.  If you "dot source" the script or function when you call it then the isolation is removed.  
$global:var
visible everywhere
$local:var
defined in this scope and visible to children
$private:var
invisible to children
$script:var
visible to this script only
$env:var
 

Add new comment