Executing powershell script on Windows machine

From Notes_Wiki
Revision as of 05:01, 22 May 2023 by Saurabh (talk | contribs) (Created page with "Home > Windows > Windows Desktop Tools or Utilities > Executing powershell script on Windows machine =Normal execution of powershell script= To execute powershell script in Windows use: * Right click on powershell script and use "Run with powershell" option OR * Open a powershell Window. Run powershell script with full / relative path. Eg "cd C:\Users\Example\Downloads & .\Script1.ps" =Changing powershell Execution Policy= However, executio...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Home > Windows > Windows Desktop Tools or Utilities > Executing powershell script on Windows machine

Normal execution of powershell script

To execute powershell script in Windows use:

  • Right click on powershell script and use "Run with powershell" option

OR

  • Open a powershell Window. Run powershell script with full / relative path. Eg "cd C:\Users\Example\Downloads & .\Script1.ps"


Changing powershell Execution Policy

However, execution might depend upon execution policy. If you get Authorization Error while running a powershell script, you may need to adjust the PowerShell execution policy to enable running any PowerShell script. The execution policy determines the level of security for running PowerShell scripts on a system. By default, Windows 10 sets the execution policy to a more restrictive setting, which may prevent running scripts. To enable running any PowerShell script use:

  1. Open PowerShell: Press the Windows key, type "PowerShell," and select "Windows PowerShell" or "Windows PowerShell (Admin)" to open PowerShell with administrative privileges.
  2. Check the Current Execution Policy: In the PowerShell window, run the following command to check the current execution policy:
    Get-ExecutionPolicy
    This will display the current execution policy. If it is set to "Restricted" or "RemoteSigned," you will need to change it to allow running any script.
  3. Change the Execution Policy: To change the execution policy, use the following command:
    Set-ExecutionPolicy Unrestricted
    This command sets the execution policy to "Unrestricted," which allows running any PowerShell script without any restrictions. You can also use the "Bypass" option if you want to temporarily bypass the execution policy for the current session.
    Note: Changing the execution policy to "Unrestricted" can pose security risks. Make sure you understand the implications and only enable it if you trust the scripts you run.
    Confirm the Change: After running the command, you will be prompted to confirm the change. Type "Y" and press Enter to confirm.
  4. Verify the Execution Policy: Run the following command again to verify that the execution policy has been changed:
    Get-ExecutionPolicy
    It should now display "Unrestricted" as the current execution policy.

Refer:


Authorization Error while trying to change Execution Policy

If you encounter an authorization error while attempting to run the Set-ExecutionPolicy Unrestricted command, it means that you don't have sufficient permissions to change the execution policy. In such cases, you can try the following alternative approaches:

Run as Administrator

  1. Run PowerShell as Administrator: Right-click on the PowerShell icon or the Start menu entry for PowerShell and select "Run as Administrator." This will launch PowerShell with elevated privileges, providing the necessary authorization to change the execution policy. Then, try running the Set-ExecutionPolicy Unrestricted command again.

Change Group Policy

Use Group Policy (for Windows 10 Pro or Enterprise): If you are using Windows 10 Pro or Enterprise edition, you can modify the execution policy via Group Policy. Follow these steps:

  1. Press Windows + R to open the Run dialog box, type gpedit.msc, and hit Enter. This will open the Local Group Policy Editor.
  2. Navigate to "Computer Configuration" > "Administrative Templates" > "Windows Components" > "Windows PowerShell."
  3. Double-click on "Turn on Script Execution."
  4. Select the "Enabled" option, choose "Allow all scripts" in the "Execution Policy" dropdown, and click "OK."
  5. Close the Local Group Policy Editor.
  6. After applying the Group Policy, the execution policy for PowerShell should be set to "Unrestricted." Open a new PowerShell session to confirm the change.

Use Scope parameter

Use the -Scope Parameter: If you have a specific script that you want to run, you can bypass the execution policy for that session only using the -Scope parameter. Open PowerShell with administrative privileges and run the following command:

    PowerShell -ExecutionPolicy Bypass -File "path_to_script.ps1"

Here, replace "path_to_script.ps1" with the actual path to your PowerShell script. This allows you to run the specific script without modifying the global execution policy.

If none of these approaches work, it's possible that your system administrator has restricted the ability to change the execution policy using anti-virus or end-point security. In such cases, you may need to contact your IT department or system administrator to request the necessary permissions or policy changes.


Home > Windows > Windows Desktop Tools or Utilities > Executing powershell script on Windows machine