Powershell: Cmdlet Aliases, Automatic Variables and other Shortcuts

Some notes on the items listed in the title 😉

Comments
Use # ... for single-line comments.
Use <# ... #> for multi-line/block (or embedded!) comments.
Set-StrictMode
Set-StrictMode -Version Latest enables and enforces coding rules and best practices in expressions, scripts, and script blocks.

By default, Set-StrictMode is turned off to prevent error messages while you are writing the script.
When strict mode is on, PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.

Set-StrictMode affects only the current scope and its child scopes.
Therefore, you can use it in a script or function to override the setting inherited from the global scope.\

  • -Off turns the strict mode off again.

  • -Version <1.0|2.0|3.0|Latest>.
    The latest version is the most strict. Use this value to make sure that scripts use the strictest available version, even when new versions are added to PowerShell.

    Caution: The meaning of Latest can change in new releases of PowerShell. Meaning, a script written for an older version of PowerShell that uses the latest version is subject to more restrictive rules when run in a newer version of PowerShell.

Tip: Put this in your Powershell profile file.

$PSVersionTable
Holds information about the version of your Powershell instance.
$PWD
Contains a path object that represents the full path of the current working directory.
The path to the script itself or its parent folder
Note that these work only in a script or function, but not on the shell/console itself.
---- test.ps1 -----------------------------
"1: " + $script:MyInvocation.MyCommand.Path
"2: " + $PSScriptRoot
"3: " + $PSCommandPath
-------------------------------------------

Output of the script:

> C:\test\test.ps1
1: C:\test\test.ps1
2: C:\test
3: C:\test\test.ps1

Item 1 and 3 look identical, but the variables have very different types:

> $MyInvocation.GetType().FullName
System.Management.Automation.InvocationInfo

> $PSScriptRoot.GetType().FullName
System.String

> $PSCommandPath.GetType().FullName
System.String
% { ... }
Shortcut for ForEach { ... }, which in turn is a shortcut for ForEach-Object { ... }.
? { ... }
Shortcut for Where { ... }, which in turn is a shortcut for Where-Object { ... }.
$_ or $PSItem
The current object in the pipeline object; or in a Param() block part like [ValidateScript({...})].
$PSBoundParameters
A hashtable containing the parameters passed to a script or a function. Only includes the values of parameters that were explicitly supplied, not those that have been left with a default value!
$Args
An array of values for undeclared parameters that are passed to a function, script, or script block.
$Input
Contains an enumerator [not an array! Rather a kind of an ‘iterator’ in C++ lingo, I’d say –so] that enumerates all input that is passed to a function. The $input variable is available only to functions and script blocks (which are unnamed functions).
  • In a function without a Begin, Process, or End block, the $input variable enumerates the collection of all input to the function.
  • In the Begin block, the $input variable contains no data.
  • In the Process block, the $input variable contains the object that is currently in the pipeline.
  • In the End block, the $input variable enumerates the collection of all input to the function.

Note: You cannot use the $input variable inside both the Process block and the End block in the same function or script block.

Since $input is an enumerator, accessing any of it’s properties causes $input to no longer be available. You can store $input in another variable to reuse the $input properties.

Enumerators contain properties and methods you can use to retrieve loop values and change the current loop iteration. For more information, see Using Enumerators.

$OFS
The Output Field Separator is an Automatic Variable in Powershell which contains a value that is used when converting an array to a string.

By default, $OFS is " " (a space character), but that can be changed:
$private:OFS = "" # nothing
$private:OFS = ";" # semicolon

Example:

> $a = 1, 2, 3
> $a
1
2
3
    
> [string] $s = $a
> $s
1 2 3
    
> $private:OFS = ";"
> [string] $s = $a
> $s
1;2;3