🎓 A1Learner
🔍
0 of 46 lessons completed

PowerShell Constrained Language

A language mode is a setting that controls which language features a session may use. Constrained language is one such mode, and it blocks the riskiest features while ordinary commands still run.

Read the current language mode

A session can read its own language mode, but it cannot be changed from inside a lesson block. This Linux session reports its mode directly.

$ExecutionContext.SessionState.LanguageMode

Test the mode by name

The mode can be compared with a name to test which mode is active. This comparison checks for FullLanguage, the mode this session uses.

$ExecutionContext.SessionState.LanguageMode -eq "FullLanguage"

Describe the mode in plain words

An if statement can turn the mode name into a plain description. Only the branch that matches the real mode prints.

if ($ExecutionContext.SessionState.LanguageMode -eq "FullLanguage") {
  "Full language mode: all language features are allowed."
} else {
  "A restricted mode is active."
}

Example

$mode = $ExecutionContext.SessionState.LanguageMode
$mode
if ($mode -eq "FullLanguage") {
  "Ordinary commands run, and the riskiest features are not blocked here."
} else {
  "Ordinary commands run, and the riskiest features are blocked."
}

Key points

  • Language mode: a setting that controls which language features a session may use.
  • FullLanguage: the mode that allows all language features.
  • ConstrainedLanguage: a mode that blocks the riskiest features while ordinary commands still run.
  • A session reads its own mode from $ExecutionContext.SessionState.LanguageMode.
  • A mode is read only in this lesson. It is never changed here.

In this example, the session mode is first read into a variable and printed. An if statement then prints the plain meaning of the mode that is actually active. The mode itself is only read, never changed.

Note

This Linux session runs in FullLanguage, so the blocks below read the mode and describe it. They never claim a constrained session was created.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does a session language mode describe?