Security Power Level: Advanced Chapter 17: PowerShell Security and Hardening
Constrained language mode
Language mode controls how much of the PowerShell language is available in a session. Constrained language mode narrows that set, so risky language features are not available to code running in that session.
Lesson 2 of 4 in Chapter 17: PowerShell Security and Hardening. Previous step in this path: Execution policy and script signing. 3 learning objectives, 5 examples, and a knowledge check.
Start: what you will learn
Language mode controls how much of the PowerShell language is available in a session. Constrained language mode narrows that set, so risky language features are not available to code running in that session.
Learning objectives
- Explain what a language mode is
- Check the current language mode
- Explain when constrained language mode is used
Concept in simple words
What it is: Language mode is a setting for a PowerShell session that controls which parts of the language may be used. A session is one running copy of PowerShell. In full language mode, the whole language is available. In constrained language mode, only a smaller, safer set of language features is available. The mode belongs to the session, not to the script file, so two sessions on the same computer can have different modes.
The four modes in simple terms: FullLanguage means all language elements are allowed, and it is the normal mode for everyday learning and admin work. ConstrainedLanguage means a smaller set is allowed on purpose, with approved cmdlets and basic language elements still working. RestrictedLanguage is even more limited, and it is used in narrow cases such as processing module manifests, where script blocks are not allowed. NoLanguage is the most limited mode, where only command calls are allowed and no script text is allowed, and it is used for very locked down endpoints. You do not need to memorise every detail of the two most limited modes, but you should recognise their names so they do not confuse you later.
Why it works
Why it exists: Some tasks need only simple commands, such as reading a value or running an approved command. If a session only needs simple work, there is no reason to give it the full power of the language. Narrowing the language lowers the damage that harmful or mistaken code can do in that session. This is the same idea as least privilege, applied to the language itself: give the session only what the job needs, and nothing extra.
What PowerShell is doing behind the setting: Language mode is a property of the session configuration, which is also called an endpoint. When a session starts under an application control policy, PowerShell can place that session in ConstrainedLanguage mode. In that kind of session, approved cmdlets still work, and basic language elements still work, but direct use of many .NET types and COM objects is limited. A script that is allowed by the policy and signed by a trusted publisher may run in FullLanguage mode in that same environment, which shows that the mode is about trust and context, not about punishing the user.
Think of it like this
Think of it like this: Full language mode is like a full toolbox. Constrained language mode is like a small kit with only the tools needed for one job. The small kit is not worse, it is chosen on purpose, because fewer tools mean fewer chances for a costly mistake. If the job needs a tool that is not in the small kit, the answer is not to force the kit open. The answer is to check whether the job belongs in a different session, or whether an approved tool can do the same job.
What still works in constrained mode: Simple, approved commands still work. Reading values still works. Running cmdlets that your session allows still works. Comparing values, showing output with Write-Output, and storing a value in a variable for a simple check still work in the examples in this lesson. What is limited is the deeper language power, such as creating many kinds of .NET objects directly, creating COM objects, and calling methods on types that are not approved. If a command fails in a constrained session, the first question is not whether your spelling is wrong. The first question is whether that feature is limited in this session.
How it works, step by step
How it works, step by step, in order: First, check the mode with $ExecutionContext.SessionState.LanguageMode. Second, read the one word result and say it out loud in your head, so you do not guess it later. Third, plan your work inside what that mode allows. Fourth, if a command needs a feature that is blocked, stop and ask an administrator what the approved way to do the job is. Fifth, if you write a script for a limited session, put a clear mode check at the top, so the script explains the limit instead of failing in a confusing way. This lesson teaches you to check and respect the mode, not to change it.
Worked example 1, checking the mode: In $ExecutionContext.SessionState.LanguageMode, $ExecutionContext is a built in variable that holds facts about the current session, SessionState describes that session, and LanguageMode names the mode. You type the whole expression and press Enter, and PowerShell shows one word, the current mode. If it shows FullLanguage, the full language is available in this session. If it shows ConstrainedLanguage, you know the session is limited on purpose. If you check the mode in a different window, you may get a different word, because the mode belongs to that session.
Worked example 2, a clear message for a limited session: In if ($ExecutionContext.SessionState.LanguageMode -eq 'ConstrainedLanguage') { Write-Output 'This session is limited.' }, the if part tests a condition, -eq means equals, and the text in single quotes is the exact mode word to compare against. If the mode matches, Write-Output shows a short message. If the mode does not match, nothing is shown, because the condition is false. Look for the message only when the mode really is ConstrainedLanguage. If you change -eq to a different comparison, the logic flips, and the message will show at the wrong time, so keep -eq for an exact match test.
Worked example 3, testing for full language mode: In $ExecutionContext.SessionState.LanguageMode -eq 'FullLanguage', the left side reads the current mode, -eq means equals, and 'FullLanguage' is the exact word to compare against. Look for True or False in the output. True means the session is in full language mode. False means it is in some other mode, not that PowerShell is broken. If you change 'FullLanguage' to 'ConstrainedLanguage', you are asking a different question, and True will now mean the session is limited, so read the word you compared against before you read the answer.
Worked example 4, testing for not full language mode: In if ($ExecutionContext.SessionState.LanguageMode -ne 'FullLanguage') { Write-Output 'This session is not in full language mode.' }, -ne means not equals, which is the opposite of -eq. This test is useful when your script needs the full language, because it catches every other mode in one test, including ConstrainedLanguage, RestrictedLanguage, and NoLanguage. Look for the message whenever the mode is anything other than FullLanguage. If you change -ne back to -eq, the message will show only for full language mode, which is the opposite of what this guard is meant to do, so check that small operator with care.
Worked example 5, storing the mode in a variable: In $mode = $ExecutionContext.SessionState.LanguageMode followed by Write-Output $mode, the first line reads the mode and stores it in a variable named $mode, and the second line shows the stored value. Look for the same one word result as the direct check, such as FullLanguage. Storing the value is useful when you need to test it more than once, because you read the session once and reuse the stored word. If you change the variable name from $mode to another name, you must change it in both lines, or the second line will show an empty value instead of the mode.
What changes if you change one part: Change -eq to -ne and the test gives the opposite answer. Change 'ConstrainedLanguage' to a misspelled word and the test will never match, even in a limited session, because the comparison needs an exact word. Change $ExecutionContext to a plain variable name and PowerShell will not know you mean the built in session facts, so the check will fail or show nothing useful. Change the message text and only the words change, not the logic, which is why the message should describe the limit clearly, not just say that something went wrong.
Edge cases that catch beginners: A script that works in your learning session may fail in a limited session, and that failure is not proof that the script is broken in general. It may be proof that the script needs a feature the limited session does not allow. An error that mentions a type, a method, or a COM object in a limited session is a strong hint to check the mode first. A session that allows simple cmdlets is not the same as a session that allows everything, so do not conclude from one working cmdlet that the whole language is available. Always check the mode word before you draw a conclusion.
Execution policy is not the same as language mode: Execution policy is about which script files may run. Language mode is about which language features are available inside a session that is already running. A session can allow script files and still limit the language, and a session can be in full language mode and still block an unsigned script file under a strict policy. Keep these two ideas in separate boxes in your mind. When something is blocked, ask first whether the block is about the file, or about the language inside the session, because the answer points you to the right check.
When to use it
When to use it, and when not to: Constrained language mode is used where sessions should stay limited, such as locked down admin tools or sessions that run code from less trusted sources. Do not use it as your everyday learning mode if your lesson needs the full language, and do not treat it as a puzzle to work around. If it blocks your task, that block is a signal to use the approved tool or ask for help. A good administrator choice is boring on purpose: the session does the approved job, and nothing extra, and that boring result is the success.
How to write a script that behaves well in a limited session: Put the mode check at the top, before any code that needs the full language. Show a clear message that names the mode and the limit. Offer the next step, such as asking an administrator or using the approved tool. Avoid code that assumes a .NET type or a COM object will always be available. Prefer simple cmdlets and basic language elements when the job allows it, because those are the parts most likely to work in a limited session. A clear early message saves a long confusing failure later.
How to spot a mistake in your own thinking: If you assume constrained mode means nothing can run, pause, because simple approved commands still work. If you guess the mode instead of checking it, pause, because one word from the check removes the guess. If you treat a blocked feature as an error in your code, pause, because the session may be limited on purpose. If you feel tempted to find a way around the limit, pause again, because the limit is a safety choice made for that session, and the right move is to ask for the approved way.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
$ExecutionContext.SessionState.LanguageModeExample 1
$ExecutionContext holds facts about the current session. SessionState describes that session, and LanguageMode shows the current mode as one word. Look for FullLanguage or ConstrainedLanguage in the output.
$ExecutionContext.SessionState.LanguageModeIllustrative output: FullLanguage
Example 2
This test compares the current mode with the word ConstrainedLanguage. The -eq operator means equals. If the mode matches, Write-Output shows a short message, so your script can explain the limit clearly instead of failing in a confusing way.
if ($ExecutionContext.SessionState.LanguageMode -eq 'ConstrainedLanguage') {
Write-Output 'This session is limited.'
}Illustrative output: This session is limited.
Example 3
The left side reads the current mode, -eq means equals, and FullLanguage is the exact word to compare against. Look for True when the session is in full language mode, and False for any other mode.
$ExecutionContext.SessionState.LanguageMode -eq 'FullLanguage'Illustrative output: True
Example 4
The -ne operator means not equals, which is the opposite of -eq. This guard catches every mode other than FullLanguage in one test, so it is useful when a script needs the full language.
if ($ExecutionContext.SessionState.LanguageMode -ne 'FullLanguage') {
Write-Output 'This session is not in full language mode.'
}Illustrative output: This session is not in full language mode.
Example 5
The first line reads the mode and stores it in a variable named $mode. The second line shows the stored value. Use this form when you need to test the mode more than once, and change the name in both lines if you rename the variable.
$mode = $ExecutionContext.SessionState.LanguageMode
Write-Output $modeIllustrative output: FullLanguage
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Assuming constrained mode means nothing can run, when simple approved commands still work.
- Guessing the mode instead of checking it with $ExecutionContext.SessionState.LanguageMode.
- Treating a blocked feature as an error in your code, instead of recognising that the session is limited on purpose.
- Assuming the mode from one window applies to every window, when the mode belongs to each session separately.
- Confusing ConstrainedLanguage with RestrictedLanguage or NoLanguage, when each mode allows a different and smaller set of features.
- Writing a script that assumes full language mode, without a clear mode check and message at the top for a limited session.
- Misspelling the mode word in a comparison, so the test never matches even when the session really is limited.
Knowledge check
Knowledge check
How do you check the current language mode?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: Language mode sets how much of the language a session may use, constrained mode allows a smaller set on purpose, you check it with $ExecutionContext.SessionState.LanguageMode, and a blocked feature in a limited session is a signal to use the approved tool or ask for help, not a puzzle to work around. FullLanguage is normal for learning, ConstrainedLanguage is normal for locked down work, and knowing which session you are in keeps your expectations honest.
Next step: Logging and transcription