Blog / What should you do if script execution is disabled?

What should you do if script execution is disabled?

by SW Team

In the world of developers, we will always encounter a variety of situations, from updating a web server version to creating a script to automate tasks. If you have ever encountered the need to run scripts on Windows, it is likely that you have encountered a console error that has prevented you from doing so.

cta:cloud_so_windows_10

How is the error displayed on the console?

For this case, a simple script called "Script_Windows.ps1" has been developed, which displays a message on the screen. The content of this script is as follows:

Write-Host "Hola SWUser!"

To run this script, you need to open the PowerShell console and type the symbol "&" followed by the path to the file and the file name, all enclosed in quotes, except for the leading symbol:

& "C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\SWUser\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Desktop\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Script_Windows.ps1"

Usually, the error in the console is a message that will be displayed as follows when executing the script.

info & : Unable to load file C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\SWUser\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Desktop\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Script_Windows.ps1 because script execution is disabled on this system. For more information, see the about_Execution_Policies topic at https:/go.microsoft.com/fwlink/?LinkID=135170.

powershell

Discover how to allow script execution in a few steps

To be able to execute scripts, it is necessary to modify the Windows script execution settings, as they are disabled by default to prevent the execution of malicious scripts on the system.

Learn about the different execution policies in Powershell

PowerShell has a number of different execution policies, four of the most popular of which are listed below:

  1. Restricted: No script will be executed. Default setting.

  2. RemoteSigned: Allow local scripts and remote signed scripts.

  3. AllSigned: Allow only signed scripts. Locally created scripts are supported.

  4. Unrestricted: Allow all scripts.

To view the current execution policy, you need to open PowerShell as an administrator. To do this, simply click Start, type "Windows PowerShell", right-click on the application and select the option "Run as administrator".

powershell

After opening the application, we will proceed to execute the following command:

Get-ExecutionPolicy -List 

powershell

This indicates that the execution policy has not been set. To fix this, run the following command:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

powershell

When we check the permissions again, we will notice that the result has been modified and that for the current user, the execution policy is "RemoteSigned".

powershell

Finally, if we run again the script that showed us the error before, we will be able to see that now it can be executed correctly.

powershell

In conclusion, Windows demonstrates its commitment to security by not allowing the execution of third-party scripts by default, thus preventing potential malicious threats to the operating system. Furthermore, it is worth noting that enabling script execution does not require advanced technical knowledge, allowing users to customize their schedules according to their preferences without much difficulty.

If you have already learned how to modify script execution policies and are ready to move forward, we invite you to explore our offerings. At SWHosting, you will find the perfect promotion for you. Discover our domain and cloud options to choose the one that best suits your projects. With competitive prices and a wide selection, visit us and start your way to online success!

cta:cloud_so_windows_sw_2019_standard

i