Operating System

How to Enable PowerShell Scripts in Windows 11: A Step-by-Step Guide

PowerShell is a tool that Microsoft developed for managerial tasks that does commands, and the script is a collection of PowerShell commands that are stored into a text file with a .ps1 file extension. During the performance of the PowerShell script, the interpreter reads the file and runs the command. Here you may ask yourself how do I enable a PowerShell Script in Windows 11. In this article, we are going to tell you the ways to enable a PowerShell Script in Windows 11.

A Quick Guide to Run a PowerShell Script

Follow the steps below to run a PowerShell script:

  1. Go to the File Explorer.
  2. Right click on the PowerShell script.
  3. The script will run the policy of Bypass. The executive policy does not change for the computer or user.

How to Make PowerShell Scripts

You can use different ways to make PowerShell scripts:

  1. You can use cross-platform code editor Visual Studio Code.
  2. You can use the PowerShell ISE.
  3. You can also use other text editors like NotePad.

How to Create PowerShell Scripts via Visual Studio Code

You should download Visual Studio Code from the Visual Studio website because it is not available out of the box on Windows. Also, we should download the Windows distribution, and it is available for 64-bit, 32-but, and ARM systems. The first thing that you should do is to open Visual Studio Code from the Start Menu.

How to Install the PowerShell Extension

  1. Click on the extension icon to open the extensions menu, or you can press Ctrl + Shift + X instead.
  2. In the Search Box, type powershell.
  3. Choose the most downloaded option, and make sure you download the extension provided by Microsoft.
  4. Select Install.

Note: After completing the installation, you will not see any confirmation. But, you will see the Install option.

How to Create a PowerShell Script through Visual Studio Code

Follow the steps below to create a PowerShell Script with Visual Studio Code:

  1. Open File.
  2. Select New File or press Ctrl + N instead.
  3. Select Plain Text on the lower left side to specify the new file is a PowerShell Script, or choose language in the script pane.
  4. Both options will send you to the box of Select a Language Mode.
  5. Type powershell.
  6. Select PowerShell.
  7. It will operate PowerShell Extension, and you can find a change in behavior and functionality. You will understand the icon next to File Name will change to PowerShell. You will also notice that the PowerShell Console will start, and that is where you can execute PowerShell commands and scripts.

You can achieve the same result through save the file with a .ps1 extension.

  • In the script pane, type the following command, and you can see the advantages like color coding, syntax hints, and command auto-completion.

1. Write-Host “This is a Visual Studio Code script”

2. Write-Host “Writing PowerShell Scripts is fun!”

  • To save file,
  • Go to the File Menu.
  • Select File.
  • Hit Save.

Note: you can use Ctrl + S instead.

  1. Find an easy-to-access location, make a file name, ensure to specify .ps1 as the file extension, and click Save.

How to Create Scripts with the Windows PowerShell ISE

To run the PowerShell ISE, follow the steps below:

  1. Go to the Start Menu.
  2. Type PowerShell ISE to see it in the search results with different opening options.

Note: It is advisable to use Run as Administrator to make sure that all command will be performed correctly.

To create a script in the PowerShell ISE, write the following lines, and you can understand handy features like color coding, syntax hints, and command auto-completion.

  1. Write-Host “This is a PowerShell ISE script”
  2. Write-Host “Writing PowerShell Scripts is fun!”

To save the list of commands as a PowerShell script, follow the steps below:

  1. On the toolbar, click on the floppy disk icon.
  2. Select File.
  3. From the file menu, click on Save.

Note: you can use Ctrl + S instead.

In the Save as dialog:

  1. Select a folder.
  2. Choose a file name.
  3. Specify the .ps1 extension.
  4. Hit Save.

How to Create Scripts with NotePad

Note: It is better to use a basic text editor to write PowerShell scripts.

Follow the steps below to create Scripts with NotePad:

  1. Enter NotePad.
  2. Type following commands:

1. Write-Host “This is a Notepad script”

2. Write-Host “Writing PowerShell Scripts is fun!

Follow the steps below to save the script:

  1. Select File.
  2. Hit Save.

In the Save as dialog, follow the steps below:

  1. Choose a folder.
  2. Choose a file name with the .ps1 extension.

How to Run PowerShell Scripts

You can run a PowerShell Script (.ps1 file) in the PowerShell console. It recognizes the .ps1 file type and performs the commands. You can use some code editors like the PowerShell ISE and Visual Studio Code to open the script and run it wholly or partially in the console pane.

How to Run PowerShell Script through Changing the Execution Policy

A safety feature that handles the conditions of loading PowerShell configuration files running scripts is PowerShell execution policies, which can help you to protect users from executing scripts unintentionally. The default execution policy for Windows 11 is Restricted, but for non-Windows OSes, the default execution policy is Unrestricted, and you cannot change it.

Use the following PowerShell command to view the current extension policy:

Get-ExecutionPolicy

Use the following command if you want to change the execution policy on Windows:

Set-ExecutionPolicy -ExecutionPolicy <PolicyName>

How to Change the PowerShell Execution Policy

You can set the execution policy for the local machine, the PowerShell process or the current user on Windows.

You can use Get-ExecutionPolicy –List to list all the available scopes.

Get-ExecutionPolicy –List

In this list, the order is important. Therefore, the highest configured item has the priority.

PolicyDescription
RestrictedThe default execution policy for Windows client OSes. It does not let ANY scripts (*.ps1 files) to be executed. But, you might run individual commands.
RemoteSignedThe default execution policy for Windows Server, which lets running scripts that are created locally. Scripts downloaded from untrusted locations, like the Internet, e-mail, messengers, etc. must be digitally signed by a trusted publisher. You may use the command Unblock-File to let a script to run on the system.
UnrestrictedThe default execution policy for non-Windows computers, and it cannot be changed. It lets unsigned scripts to run, but it shows a warning message and looks for confirmation if scripts are coming from an untrusted location.
AllSignedIt needs all scripts running on the machine to be digitally signed by a trusted publisher, no matter if they are made locally on the machine or downloaded from the Internet.
BypassIt lets all scripts to run, like Unrestricted, but no confirmation is required.
UndefinedThere is no execution policy set on the specified scope. If all scopes are set as undefined, then the default execution policies are applied.

To let PowerShell run scripts, follow the steps below:

  1. Use the following command:

Set-ExecutionPolicy -ExecutionPolicy <PolicyName>

The command, by default, applies the policy that you have chosen to the Local Machine Scope. You must use the Parameter-Scope and choose the name for the scope if you want to specify a different scope.

As an example, the following command sets the execution policy to Unrestricted for the current user:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

How to Run a Script with PowerShell

To do so, follow the steps below:

  1. Go to the Start Menu.
  2. Type powershell.
  3. Hit Run as Administrator.

Also, you can use the full path to script, like C:\TEMP\MyNotepadScript.ps1, or use just the script name from the folder where the file is located like .\MyNotepadScript.ps1 to run a script in the PowerShell Console.

How to Run a PowerShell Script in Visual Studio Code

Follow the steps below to run a script in Visual Studio Code:

  1. Start the application.
  2. Open the script file you made before.

You can perform the loaded script in VS Code by selecting Run in the top right corner of the screen or by pressing F5. Then, the script will perform on the console pane, and you can see the output of the script.

Running only one part of the script, which is called Run Selection, is another option. This option is useful when you are making your script, and you want to confirm that parts work properly.

To run a part of the script, follow the steps below:

  1. Highlight the part you want to perform.
  2. Select Run Selection in the top right corner of the console or hold F8.

This time, the VS Code performs the selected lines.

How to Run a Script in the PowerShell ISE

Follow the steps below to run a script in Visual Studio Code:

  1. Run the PowerShell ISE.
  2. Use File > Open, or clicking on the Open icon from the toolbar, or hold Ctrl + O to open the script.
  3. Select the script file and open it.

To perform the whole script, Use Run button on the toolbar of press F5.

To run a part of the script, follow the steps below:

1.            Highlight the part you want to perform.

2.            Select Run Selection in the top right corner of the console or hold F8.

This time, the PowerShell ISE performs the selected lines.

How to Run a PowerShell Script from the Command Prompt

In the case the PowerShell interprets .ps1 files, the Command Prompt (CMD) cannot work with the PowerShell script directly. You should execute it by calling the PowerShell process with the –File parameter like the below example:

PowerShell -File C:\TEMP\MyNotepadScript.ps1.

Conclusion

In this article, we presented some ways to enable script in Windows 11. From the above methods, which method will you choose? Do you know any other method? Share your answers with us.

Reyhane A

Reyhane is an author and technology enthusiast who has a passion for writing about Windows tips and tricks. Born and raised in Tehran, Iran, Reyhane always had an interest in technology, which led her to pursue a degree in computer engineering from the University of Tehran. After completing her studies, Reyhane began working as a software engineer, where she gained a wealth of experience and knowledge in the field of technology. She soon discovered her love for writing, and began sharing her knowledge with others through her blog. Reyhane's blog quickly gained popularity, and she became known as an expert in the field of Windows tips and tricks. Her articles are known for being clear, concise, and easy to understand, making them accessible to everyone, regardless of their level of technical expertise. Today, Reyhane continues to share her knowledge with the world through her blog and YouTube channel, where she provides tips and tricks for Windows users to help them optimize their computer's performance and get the most out of their technology. In addition to her passion for technology, Reyhane enjoys traveling, photography, and spending time with her family and friends. She is also an avid reader and is always on the lookout for new books to add to her collection. Reyhane's dedication to her work and her love for technology have made her a respected figure in the industry, and she continues to inspire and educate others through her writing and online content.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button