`ConstrainedLanguage` mode: `[pscustomobject]` literals cannot be used, even though the type is white-listed, and an equivalent `New-Object` call succeeds
Author: mklement0Created Nov 23, 2023Updated Sep 27, 2026
LabelsNeeds-Triage
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
# From a regular session (with language mode 'FullLanguage')
# CAVEAT: You won't be able to undo this in the same session.
$ExecutionContext.SessionState.LanguageMode = 'ConstrainedLanguage'
[pscustomobject] @{ foo = 1 }Perhaps I'm missing something, but the fact that hashtable literals alone (without the [pscustomobject] "cast") work fine, and that [pscustomobject] is among the whitelisted types, as well as the fact that the equivalent New-Object pscustomobject -Property @{ foo = 1 } does work, leads me to believe that this is a bug (it isn't specific to 7.4.0 - I see it in 7.3.8 too, and even in Windows PowerShell).
Expected behavior
A [pscustomobject] instance should be constructed and output.
Actual behavior
An error occurs (see below).
Error details
Curiously, the error message depends on whether at least one such literal was defined earlier, while the session was still in FullLanguage mode:
- In the latter case - note the confusing reference to restricted mode:
InvalidArgument: Cannot convert value "System.Collections.Specialized.OrderedDictionary" to type "System.Management.Automation.LanguagePrimitives+InternalPSCustomObject".
Error: "Cannot convert hashtable to an object of the following type: System.Management.Automation.LanguagePrimitives+InternalPSCustomObject.
Hashtable-to-Object conversion is not supported in restricted language mode or a Data section."- If no
[pscustomobject]literal was defined before entering constrained mode:
InvalidArgument: Cannot convert value to type "System.Management.Automation.LanguagePrimitives+InternalPSCustomObject".
Only core types are supported in this language mode.Environment data
PowerShell 7.4.0Visuals
No response
Source: PowerShell/PowerShell