Home OS Windows How to create and run PowerShell script file on Windows 10

How to create and run PowerShell script file on Windows 10

A script is only a assortment of instructions saved right into a textual content file (utilizing the particular .ps1 extension) that PowerShell understands and executes in sequence to carry out completely different actions. In this submit, we are going to define the method concerned in how one can create and run a PowerShell script file on Windows 10.

PowerShell is a command-line instrument designed by Microsoft to run instructions and scripts to alter settings and automate duties. In a manner, it’s much like Command Prompt. However, PowerShell is a extra succesful command-line interface (CLI) that gives an intensive set of instruments and extra flexibility and management. Also, not like Command Prompt, PowerShell is offered on Windows, macOS, and Linux.

To see how one can create and run the PowerShell script file on Windows 10, observe the directions for every of the duties as proven within the respective sections.

How to create a PowerShell script file

On Windows 10, you possibly can create PowerShell script recordsdata utilizing just about any textual content editor or the ISE (Integrated Scripting Environment) console. However, the popular choice to construct scripts shifting ahead is to make use of the Visual Studio Code editor with the PowerShell extension.

Visual Studio Code — also called VS Code — is a free and extensible cross-platform code editor that gives an setting to edit just about any sort of programming language. And when including the PowerShell extension, you get a totally interactive scripting enhancing expertise, even with IntelliSense (code-completion) assist.

Here’s how one can create PowerShell script file on Windows 10 utilizing VS Code:

  • Head to Visual Studio Download page.
  • Click the Windows button to obtain the installer.
  • Double-click the downloaded file to start out the set up technique of VS Code.
  • Confirm the settlement phrases.
  • Click the Next button.
  • Click the Next button once more.
  • Click the Next button yet one more time.
  • Confirm further duties as mandatory.
  • Click the Next button.
  • Click the Install button.
  • Click the Finish button.

Once you full the steps, you possibly can proceed to put in the PowerShell extension. Here’s how:

  • Open VS Code.
  • Click the Extensions tab from the left pane or press the CTRL + SHIFT + X key combo.
  • Search for PowerShell and choose the highest end result.
  • Click the Install button.

After you full the set up steps, you can begin writing PowerShell scripts utilizing Visual Studio Code. Here’s how:

  • Open VS Code.
  • Click the File menu and choose the New File possibility.
  • Click the File menu and choose the Save as an possibility.
  • In the File title subject specify a reputation for the file with the .ps1 extension — for instance, TWC_script.ps1.
  • Click the Save button.

Write a brand new, or paste the script you need to run — for instance:

Write-Host "Welcome to TheWindowsClub.com! Your first script executed successfully"

The above script will output the phrase under on the display.

Welcome to TheWindowsMembership.com! Your first script executed efficiently

You can click on the Run button from the top-right facet (or press the F5 key) to run the script.

  • Click the File menu.
  • Click the Save possibility.

Once you full the steps utilizing Visual Studio Code, the script might be able to run, however it would fail by default. This is as a result of the default PowerShell settings are all the time set to dam the execution of any script. (The solely exception is if you happen to run the contents of the script inside Visual Studio Code or PowerShell ISE.)

How to run PowerShell script file on Windows 10

To run a script file with PowerShell, you need to change the execution coverage on Windows 10.

On Windows 10, PowerShell contains 4 execution insurance policies, together with:

  1. Restricted — Stops any script from working.
  2. RemoteSigned — Allows scripts created on the machine, however scripts created on one other laptop gained’t run until they embrace a trusted writer’s signature.
  3. AllSigned — All the scripts will run, however provided that a trusted writer has signed them.
  4. Unrestricted — Runs any script with none restrictions.

Here’s how one can run PowerShell script file on Windows 10:

  • Press Windows key + X to open Power User Menu.
  • Press A on the keyboard to launch PowerShell in admin/elevated mode.
  • In the PowerShell console, kind within the command under and hit Enter.
Set-ExecutionPolicy RemoteSigned
  • Type A and hit Enter (if relevant).

Next, kind the command under and hit Enter to run the script. Make positive to alter “PATHTOSCRIPT” placeholder to the placement of your script.

& "C:PATHTOSCRIPTTWC_script.ps1"

After you full the steps, the script will run, and if it was written appropriately, you need to see its output with out points.

That’s it on how one can create and run the PowerShell script file on Windows 10!

Most Popular

Recent Comments