Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Enable Script Execution in Windows PowerShell

When working with PowerShell on Windows, you might encounter an error message stating, "Não pode ser carregado porque a execução de scripts foi desabilitada neste sistema" (translated: "Cannot be loaded because script execution is disabled on this system"). This error occurs because PowerShell has a security feature that restricts the execution of scripts to prevent malicious activities. By default, the execution policy is set to "Restricted," which prevents any scripts from running.

In this article, I will guide you through the steps to change the execution policy to allow script execution in PowerShell.

Understanding PowerShell Execution Policies

PowerShell execution policies are a safety feature that controls the conditions under which PowerShell loads configuration files and runs scripts. There are several execution policies you can set:

  • Restricted: No scripts can be run. PowerShell can be used only in interactive mode.
  • AllSigned: Only scripts signed by a trusted publisher can be run.
  • RemoteSigned: Downloaded scripts must be signed by a trusted publisher before they can be run.
  • Unrestricted: No restrictions; all scripts can be run. However, you will be prompted for permission to run scripts downloaded from the internet.
  • Bypass: Nothing is blocked and there are no warnings or prompts.
  • Undefined: Removes the currently assigned execution policy from the current scope.

How to Change the Execution Policy

To change the execution policy, you need to use the Set-ExecutionPolicy cmdlet. Here’s how you can do it:

Step 1: Open PowerShell as Administrator

  1. Click on the Start menu.
  2. Type PowerShell.
  3. Right-click on Windows PowerShell and select Run as Administrator.

Step 2: Check the Current Execution Policy

Before changing the execution policy, it’s a good practice to check the current policy. You can do this with the following command:

Get-ExecutionPolicy

Step 3: Change the Execution Policy

To change the execution policy to RemoteSigned, which is a common choice for allowing script execution while maintaining some security, use the following command:

Set-ExecutionPolicy RemoteSigned

You will be prompted to confirm the change. Type Y and press Enter.

Step 4: Verify the Change

After setting the new execution policy, verify it by running:

Get-ExecutionPolicy

This should return RemoteSigned.

Example: Running a PowerShell Script

Now that the execution policy is set to allow script execution, you can run PowerShell scripts. Here’s an example script that outputs "Hello, World!":

Create a new file named HelloWorld.ps1 with the following content:

Write-Output "Hello, World!"

To execute the script, navigate to the directory containing HelloWorld.ps1 and run:

.\HelloWorld.ps1

You should see "Hello, World!" printed in the PowerShell window.

Important Considerations

  • Always be cautious when changing the execution policy, especially on production systems.
  • Consider the security implications of allowing script execution and choose the policy that best fits your security requirements.
  • Remember that execution policies are not a security system but a way to prevent accidental execution of scripts.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.