Question How to run powershell in constrained language mode all the time?

Please provide comments and solutions that are helpful to the author of this topic.

Bot

AI-powered Bot
Verified
Apr 21, 2016
3,467
To enforce constrained language mode for PowerShell, you can do the following:

1. Run PowerShell as an administrator.
2. Execute the following command: `Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell" -Name "ExecutionPolicy" -Value "ConstrainedLanguage"`
3. Close all instances of PowerShell.
4. Open PowerShell again, and it should be running in constrained language mode.

Note: Enforcing constrained language mode may limit the functionality of PowerShell, since it restricts access to some APIs and cmdlets. Make sure to thoroughly test your scripts and ensure they are compatible with constrained language mode before enforcing it.
 
Upvote 0

Azazel

Level 5
Thread author
Jun 15, 2023
227
To enforce constrained language mode for PowerShell, you can do the following:

1. Run PowerShell as an administrator.
2. Execute the following command: `Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell" -Name "ExecutionPolicy" -Value "ConstrainedLanguage"`
3. Close all instances of PowerShell.
4. Open PowerShell again, and it should be running in constrained language mode.

Note: Enforcing constrained language mode may limit the functionality of PowerShell, since it restricts access to some APIs and cmdlets. Make sure to thoroughly test your scripts and ensure they are compatible with constrained language mode before enforcing it.
is it a dword or string?
 
  • Like
Reactions: [correlate]
Upvote 0

Bot

AI-powered Bot
Verified
Apr 21, 2016
3,467
is it a dword or string?
The "ExecutionPolicy" value is a string registry value. To enforce the constrained language mode, you need to set the value of "ExecutionPolicy" to "ConstrainedLanguage" as a string.

In PowerShell, you can set the registry value using the Set-ItemProperty cmdlet as follows:

```
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell" -Name "ExecutionPolicy" -Value "ConstrainedLanguage"
```

This command sets "ExecutionPolicy" to "ConstrainedLanguage" as a string value.
 
  • Like
Reactions: [correlate]
Upvote 0

Azazel

Level 5
Thread author
Jun 15, 2023
227
The "ExecutionPolicy" value is a string registry value. To enforce the constrained language mode, you need to set the value of "ExecutionPolicy" to "ConstrainedLanguage" as a string.

In PowerShell, you can set the registry value using the Set-ItemProperty cmdlet as follows:

```
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell" -Name "ExecutionPolicy" -Value "ConstrainedLanguage"
```

This command sets "ExecutionPolicy" to "ConstrainedLanguage" as a string value.
it didn't work
 
  • Like
Reactions: [correlate]
Upvote 0

Bot

AI-powered Bot
Verified
Apr 21, 2016
3,467
it didn't work
If setting the registry value for "ExecutionPolicy" to "ConstrainedLanguage" did not work, you may need to check if there are any other policies or configurations that are conflicting with this setting.

You can also try setting the ExecutionPolicy to "AllSigned". This policy allows you to run only signed scripts and, therefore, reduces the risk of untrusted scripts running on your system. You can set the ExecutionPolicy to "AllSigned" using the following PowerShell command:

```
Set-ExecutionPolicy -ExecutionPolicy AllSigned -Scope LocalMachine
```

This will set the ExecutionPolicy to "AllSigned" for PowerShell on the local machine. Note that you need to run this command with administrative privileges.

If none of these solutions work, please provide more details on the error message you received, and also your specific scenario and configurations.
 
  • Like
Reactions: [correlate]
Upvote 0

Azazel

Level 5
Thread author
Jun 15, 2023
227
If setting the registry value for "ExecutionPolicy" to "ConstrainedLanguage" did not work, you may need to check if there are any other policies or configurations that are conflicting with this setting.

You can also try setting the ExecutionPolicy to "AllSigned". This policy allows you to run only signed scripts and, therefore, reduces the risk of untrusted scripts running on your system. You can set the ExecutionPolicy to "AllSigned" using the following PowerShell command:

```
Set-ExecutionPolicy -ExecutionPolicy AllSigned -Scope LocalMachine
```

This will set the ExecutionPolicy to "AllSigned" for PowerShell on the local machine. Note that you need to run this command with administrative privileges.

If none of these solutions work, please provide more details on the error message you received, and also your specific scenario and configurations.
is it possible to use AppLocker to enforce contraint language mode
 
  • Like
Reactions: [correlate]
Upvote 0

Bot

AI-powered Bot
Verified
Apr 21, 2016
3,467
is it possible to use AppLocker to enforce contraint language mode
Unfortunately, it is not possible to use AppLocker to enforce constrained language mode directly. AppLocker is designed to restrict the execution of specific files based on their path, publisher, or hash, but it cannot enforce any specific PowerShell execution mode, like constrained language mode.

However, you can use AppLocker to restrict the execution of PowerShell scripts to specific paths or publishers, which can help in reducing risks associated with malicious scripts. This can also be combined with other security measures, such as setting the PowerShell execution policy to "Restricted" or "AllSigned" to further reduce the risks associated with PowerShell scripting.
 
  • Like
Reactions: [correlate]
Upvote 0

Azazel

Level 5
Thread author
Jun 15, 2023
227
It would look like this?
SystemPropertiesAdvanced_2U9q3bLcuf.png
 
Upvote 0

Andy Ful

From Hard_Configurator Tools
Verified
Honorary Member
Top Poster
Developer
Well-known
Dec 23, 2014
8,145
To enforce constrained language mode for PowerShell, you can do the following:

1. Run PowerShell as an administrator.
2. Execute the following command: `Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell" -Name "ExecutionPolicy" -Value "ConstrainedLanguage"`
3. Close all instances of PowerShell.
4. Open PowerShell again, and it should be running in constrained language mode.

Note: Enforcing constrained language mode may limit the functionality of PowerShell, since it restricts access to some APIs and cmdlets. Make sure to thoroughly test your scripts and ensure they are compatible with constrained language mode before enforcing it.

It is nonsense. There is no such ExecutionPolicy.
https://learn.microsoft.com/en-us/p...urity/set-executionpolicy?view=powershell-7.3

Unfortunately, it is not possible to use AppLocker to enforce constrained language mode directly. AppLocker is designed to restrict the execution of specific files based on their path, publisher, or hash, but it cannot enforce any specific PowerShell execution mode, like constrained language mode.

It is not true. Currently (on Windows 10 and 11), when AppLocker is set to block Scripts, the ConstrainedLanguage Mode is automatically enabled. The same is true when using Software Restriction Policies (like in H_C or SWH).
https://4sysops.com/archives/mitigating-powershell-risks-with-constrained-language-mode/

The above article explains how to enable ConstrainedLanguage Mode via AppLocer or without AppLocker (by a reg tweak).
 
Last edited:
Upvote 0

TairikuOkami

Level 35
Verified
Top Poster
Content Creator
Well-known
May 13, 2017
2,489
Not in 7, which has many security improvements, but unfortunately, Windows uses version 5 by default. I have removed version 5 and I have version 7, but I keep this policy, just in case.

Code:
taskkill /im PowerShell.exe /f
taskkill /im PowerShell_ISE.exe /f
taskkill /im pwsh.exe /f
takeown /s %computername% /u %username% /f "%ProgramFiles%\WindowsPowerShell" /r /d y
icacls "%ProgramFiles%\WindowsPowerShell" /inheritance:r /grant:r %username%:(OI)(CI)F /t /l /q /c
rd "%ProgramFiles%\WindowsPowerShell" /s /q
takeown /s %computername% /u %username% /f "%ProgramFiles(x86)%\WindowsPowerShell" /r /d y
icacls "%ProgramFiles(x86)%\WindowsPowerShell" /grant:r %username%:(OI)(CI)F /t /l /q /c
rd "%ProgramFiles(x86)%\WindowsPowerShell" /s /q
takeown /s %computername% /u %username% /f "%WinDir%\System32\WindowsPowerShell" /r /d y
icacls "%WinDir%\System32\WindowsPowerShell" /grant:r %username%:(OI)(CI)F /t /l /q /c
rd "%WinDir%\System32\WindowsPowerShell" /s /q
takeown /s %computername% /u %username% /f "%WinDir%\SysWOW64\WindowsPowerShell" /r /d y
icacls "%WinDir%\SysWOW64\WindowsPowerShell" /grant:r %username%:(OI)(CI)F /t /l /q /c
rd "%WinDir%\SysWOW64\WindowsPowerShell" /s /q

reg add "HKLM\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /v "ExecutionPolicy" /t REG_SZ /d "Restricted" /f
reg add "HKLM\Software\Microsoft\PowerShell\1\ShellIds\ScriptedDiagnostics" /v "ExecutionPolicy" /t REG_SZ /d "Restricted" /f
reg add "HKLM\Software\WOW6432Node\Microsoft\PowerShell\1\ShellIds\ScriptedDiagnostics" /v "ExecutionPolicy" /t REG_SZ /d "Restricted" /f
reg add "HKLM\Software\Policies\Microsoft\Windows\PowerShell" /v "EnableScripts" /t REG_DWORD /d "0" /f
reg add "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v "__PSLockDownPolicy" /t REG_SZ /d "4" /f
 
Upvote 0

Andy Ful

From Hard_Configurator Tools
Verified
Honorary Member
Top Poster
Developer
Well-known
Dec 23, 2014
8,145

TairikuOkami,​

There is no ExecutionPolicy named "ConstrainedLanguage" in any PowerShell version.
So, the below registry change (proposed by Bot) is still nonsense:
`Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell" -Name "ExecutionPolicy" -Value "ConstrainedLanguage"`

Of course, you do not do such nonsense in your script. :) (y)
 
Upvote 0
F

ForgottenSeer 97327

It is nonsense. There is no such ExecutionPolicy.
https://learn.microsoft.com/en-us/p...urity/set-executionpolicy?view=powershell-7.3



It is not true. Currently (on Windows 10 and 11), when AppLocker is set to block Scripts, the ConstrainedLanguage Mode is automatically enabled. The same is true when using Software Restriction Policies (like in H_C or SWH).
https://4sysops.com/archives/mitigating-powershell-risks-with-constrained-language-mode/

The above article explains how to enable ConstrainedLanguage Mode via AppLocer or without AppLocker (by a reg tweak).
In WDAC also when you don't disable script enforcement
 
Upvote 0

Andy Ful

From Hard_Configurator Tools
Verified
Honorary Member
Top Poster
Developer
Well-known
Dec 23, 2014
8,145
@TairikuOkami

How did you remove Powershell 5, I thought 5+7 co-existed?

He removed built-in PowerShell temporarily, by removing the PowerShell folders. It can be restored after some .NET Framework update.
Different versions of PowerShell can coexist, but blocking PS scripts in one version does not apply in another. How to block PS scripts also in ver. 7 is explained in the video posted in this thread.

Edit.
Instead of messing with the PowerShell 7.0 installation files one can simply add the registry key:

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\PowerShellCore]
"EnableScripts"=dword:00000000

This will automatically add the second key:

[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Policies\Microsoft\PowerShellCore]
"EnableScripts"=dword:00000000

If the attacker would like to install & run PowerShell 7.0 (to bypass the ExecutionPolicy in Windows built-in PowerShell), then the script execution will be blocked.
 
Last edited:
Upvote 0

About us

  • MalwareTips is a community-driven platform providing the latest information and resources on malware and cyber threats. Our team of experienced professionals and passionate volunteers work to keep the internet safe and secure. We provide accurate, up-to-date information and strive to build a strong and supportive community dedicated to cybersecurity.

User Menu

Follow us

Follow us on Facebook or Twitter to know first about the latest cybersecurity incidents and malware threats.

Top