technodesigner

Digital design

Olivier Dewit
PowerShell and code expansion

PowerShell and code expansion

Through a mechanism called subexpression expansion, PowerShell is able to interpret the PowerShell code integrated in a string.

Subexpression expansion

The part of the string that contains the code is called a subexpression. It is specified by the subexpression operator $(). At runtime, the contents of the subexpression is interpreted and replaced by its result. Example:

PS> "Involved processus : $(Get-Process di* | select -ExpandProperty ProcessName)"
Involved processus : DisplayLinkManager DisplayLinkUI DisplayLinkUserAgent

Caution, subexpression expansion produces only text... (as opposed to Invoke-Expression) .

The sub-expression can contain multiple expressions , while separated by a semicolon :

PS> "Involved processus : $(Get-Process dev* | select -ExpandProperty ProcessName ; Get-Process pow* | select -ExpandProperty ProcessName)"
Involved processus : devenv powershell_ise

The code of a sub-expression may consist of calling a variable :

PS> $value = 5
PS> "The value is $($value)."
The value is 5.

In this case , the subexpression operator $() can be omitted, which generally corresponds to the syntax used for variable substitution :

PS> "The value is $value."
The value is 5.

But operations on the variable still require subexpression operator $() :

PS> "Value type is $($value.GetType())."
Value type is int.

If quotation marks should appear in the resulting string, use the escape character ` :

PS> $text = "ABCD"
PS> "Length of `"$text`" is $($text.Length) characters."
Length of "ABCD" is 4 characters.

Finally, subexpression expansion requires that the string literal is enclosed in quotation marks "...". A string surrounded by single quotes is not expanded (but in return offers faster processing).

PS> 'The value is $value.'
The value is $value.

Here-string

Here-string is the PowerShell implementation of the concept of here-document (or heredoc ). It is a literal text of which all formatting is preserved, and that, in the case of PowerShell, supports subexpression expansion (which is not the case in all languages that ??support this concept). The text can be multiline, contain spaces, quotation marks, indentations, variable parts, etc.

The PowerShell here-string is delimited by the characters @" and "@ (followed by a newline).

Example :

PS> $text = @"
Sample text
  - Multiline
  - Indented
  - Supporting quotes without escape character "..."
  - Variable expansion : $PSCulture
  - And the subexpression expansion : $(Get-Process di* | select -ExpandProperty ProcessName)
"@

Expansion :

PS> Sample text
  - Multiline
  - Indented
  - Supporting quotes without escape character "..."
  - Variable expansion : fr-FR
  - And the subexpression expansion : DisplayLinkManager DisplayLinkUI DisplayLinkUserAgent

Summary

With subexpression expansion and here-string, PowerShell offers a very powerful interpretation engine, which can be used for example to generate code and even define new specialized languages, aka DSL (Domain-Specific Language)...



Powered by the Perspective WebApp - © Olivier Dewit - Lyon, Paris, France - Legal informations