Run powershell script from windows powershell

How to create and run a PowerShell script file on Windows 10

Source: Windows Central

On Windows 10, PowerShell is a command-line tool designed by Microsoft to run commands and scripts to change settings and automate tasks. In a way, it’s similar to Command Prompt. However, PowerShell is a more capable command-line interface (CLI) that offers an extensive set of tools and more flexibility and control. Also, unlike Command Prompt, PowerShell is available on Windows, macOS, and Linux.

A script is just a collection of commands saved into a text file (using the special «.ps1» extension) that PowerShell understands and executes in sequence to perform different actions.

The only caveat is that the default security protocol always blocks any script from running on a device. This means that when double-clicking a «.ps1» file on Windows 10 nothing will happen, and if you try to run the script within PowerShell, you’ll see the «cannot be loaded because running scripts is disabled on this system» error message. However, it’s not impossible to run scripts on your computer. You only need to enable the correct execution policy.

In this Windows 10 guide, we’ll walk you through the steps to successfully write and run your first script file on PowerShell using Visual Studio Code, Notepad, and the PowerShell Integrated Scripting Environment (ISE) console.

How to create PowerShell script file on Windows 10

On Windows 10, you can create PowerShell script files using virtually any text editor or the ISE console. However, the preferred option (thanks @jotaka for the heads up) to build scripts moving forward is to use the Visual Studio Code editor with the PowerShell extension.

Creating script with Visual Studio Code

Visual Studio Code — also known as VS Code — is a free and extensible cross-platform code editor that provides an environment to edit virtually any kind of programming language. And when adding the PowerShell extension, you get a fully interactive scripting editing experience, even with IntelliSense (code-completion) support.

The new experience is meant to be the new default, but the PowerShell ISE console isn’t going away. Still, the company won’t be adding any more features, and it doesn’t support PowerShell 7 or higher releases.

Install Visual Studio Code

To install Visual Basic Code on Windows 10, use these steps:

Читайте также:  Rss ������� ��� mac os

Click the Windows button to download the installer.

Source: Windows Central

Click the Next button.

Source: Windows Central

Confirm additional tasks as necessary.

Source: Windows Central

  • Click the Next button.
  • Click the Install button.
  • Click the Finish button.
  • Once you complete the steps, you can proceed to install the PowerShell extension.

    Install PowerShell extension

    To install the PowerShell extension on VS Code, use these steps:

    1. Open VS Code.
    2. Click the Extensions tab from the left pane.
    3. Search for PowerShell and select the top result.

    Click the Install button.

    Source: Windows Central

    After you complete the steps, you can start writing PowerShell scripts using Visual Studio Code.

    Create PowerShell script with Visual Studio Code

    To create a script with Visual Basic Code, use these steps:

      Open VS Code.

    Click the File menu and select the New File option.

    Source: Windows Central

    Click the File menu and select the Save as option.

    Source: Windows Central

    Write a new, or paste the script you want to run — for example:

    Write-Host «Congratulations! Your first script executed successfully»

    The above script will output the phrase «Congratulations! Your first script executed successfully» on the screen.

    (Optional) Click the Run button from the top-right side (or press the F5 key) to run the script.

    Source: Windows Central

  • Click the File menu.
  • Click the Save option.
  • Creating script with Notepad

    To create a PowerShell script using the Notepad editor on Windows 10, use these steps:

    1. Open Start.
    2. Search for Notepad, and click the top result to open the app.

    Write a new, or paste your script, in the text file — for example:

    Write-Host «Congratulations! Your first script executed successfully»

    Source: Windows Central

    Type a descriptive name for the script — for example, first_script.ps1.

    Source: Windows Central

  • Click the Save button.
  • Creating script with Integrated Scripting Environment

    Alternatively, you can use the built-in PowerShell ISE console to code your scripts on Windows 10.

    The Integrated Scripting Environment is an advanced tool, but you can get started using these steps:

    1. Open Start.
    2. Search for Windows PowerShell ISE, right-click the top result, and select the Run as administrator option.
    3. Click on File menu.

    Select the New option to create a new empty .ps1 file.

    Source: Windows Central

    Write a new, or paste the script you want to run — for example:

    Write-Host «Congratulations! Your first script executed successfully»

    Source: Windows Central

    Type a name for the script – for example, first_script.ps1.

    Source: Windows Central

  • Select the folder location to store the script.
  • Click the Save button.
  • Once you complete the steps using Notepad, Visual Studio Code, or PowerShell ISE, the script will be ready to run, but it will fail by default. This is because the default PowerShell settings are always set to block the execution of any script. (The only exception is if you run the contents of the script within Visual Studio Code or PowerShell ISE.)

    How to run PowerShell script file on Windows 10

    If you wish to run a script file with PowerShell, you have to change the execution policy on Windows 10.

    To change the execution policy to run PowerShell scripts, use these steps:

    1. Open Start.
    2. Search for PowerShell, right-click the top result, and select the Run as administrator option.

    Type the following command to allow scripts to run and press Enter:

    Type A and press Enter (if applicable).

    Source: Windows Central

    Type the following command to run the script and press Enter:

    In the above command, make sure to change «PATH\TO\SCRIPT» to the location of your script.

    For example, this command runs a script stored in the Downloads folder:

    Source: Windows Central

    After you complete the steps, the script will run, and if it was written correctly, you should see its output without issues.

    On Windows 10, PowerShell includes four execution policies, including:

    • Restricted — Stops any script from running.
    • RemoteSigned — Allows scripts created on the device, but scripts created on another computer won’t run unless they include a trusted publisher’s signature.
    • AllSigned — All the scripts will run, but only if a trusted publisher has signed them.
    • Unrestricted — Runs any script without any restrictions.

    In the above steps, we use the command to allow local scripts to run on Windows 10. However, if you’re not planning to run scripts regularly, you can restore the default settings to block untrusted scripts using the same instructions outlined above, but on step No. 4, make sure to use the Set-ExecutionPolicy Restricted command.

    More Windows 10 resources

    For more helpful articles, coverage, and answers to common questions about Windows 10, visit the following resources:

    The Dell XPS 15 is our choice for best 15-inch laptop

    For a lot of people, a 15-inch laptop is a perfect size that offers enough screen for multitasking, and in a lot of cases, some extra performance from powerful hardware. We’ve rounded up the best of the best at this size.

    Halo: MCC’s live service elements make it better, not worse

    Halo: The Master Chief Collection is more popular than ever, but some fans don’t agree with the live service approach 343 Industries has taken with it. Here’s why those elements are, at the end of the day, great for the game and for Halo overall.

    Microsoft’s Surface Duo is not ‘failing up’

    Microsoft announced this week that it was expanding Surface Duo availability to nine new commercial markets. While Surface Duo is undoubtedly a work in progress, this is not a sign of a disaster. It’s also doesn’t mean that Surface Duo is selling a ton either. Instead, the reason for the expansion is a lot more straightforward.

    These are the best PC sticks when you’re on the move

    Instant computer — just add a screen. That’s the general idea behind the ultra-portable PC, but it can be hard to know which one you want. Relax, we have you covered!

    Starting Windows PowerShell

    Windows PowerShell is a scripting engine .DLL that’s embedded into multiple hosts. The most common hosts you’ll start are the interactive command-line powershell.exe and the Interactive Scripting Environment powershell_ise.exe .

    To start Windows PowerShellВ® on Windows ServerВ® 2012 R2, WindowsВ® 8.1, Windows Server 2012, and Windows 8, see Common Management Tasks and Navigation in Windows.

    PowerShell Core has renamed binary

    PowerShell Core, referred to as PowerShell, is version 6 and higher that’s open source and uses .NET Core. Supported versions are available on Windows, macOS, and Linux.

    Beginning in PowerShell 6, the PowerShell binary was renamed pwsh.exe for Windows and pwsh for macOS and Linux. You can start PowerShell preview versions using pwsh-preview . For more information, see What’s New in PowerShell Core 6.0 and About pwsh.

    To find cmdlet reference and installation documentation for PowerShell 7, use the following links:

    Document Link
    Cmdlet reference PowerShell Module Browser
    Windows installation Installing PowerShell Core on Windows
    macOS installation Installing PowerShell Core on macOS
    Linux installation Installing PowerShell Core on Linux

    To view content for other PowerShell versions, see How to use the PowerShell documentation.

    How to Start Windows PowerShell on Earlier Versions of Windows

    This section explains how to start Windows PowerShell and Windows PowerShell Integrated Scripting Environment (ISE) on WindowsВ® 7, Windows ServerВ® 2008 R2, and Windows ServerВ® 2008. It also explains how to enable the optional feature for Windows PowerShell ISE in Windows PowerShell 2.0 on Windows ServerВ® 2008 R2 and Windows ServerВ® 2008.

    Use any of the following methods to start the installed version of Windows PowerShell 3.0, or Windows PowerShell 4.0, where applicable.

    From the Start Menu

    • Click Start, type PowerShell, and then click Windows PowerShell.
    • From the Start menu, click Start, click All Programs, click Accessories, click the Windows PowerShell folder, and then click Windows PowerShell.

    At the Command Prompt

    In cmd.exe, Windows PowerShell, or Windows PowerShell ISE, to start Windows PowerShell, type:

    You can also use the parameters of the powershell.exe program to customize the session. For more information, see PowerShell.exe Command-Line Help.

    With Administrative privileges (Run as administrator)

    Click Start, type PowerShell, right-click Windows PowerShell, and then click Run as administrator.

    How to Start Windows PowerShell ISE on Earlier Releases of Windows

    Use any of the following methods to start Windows PowerShell ISE.

    From the Start Menu

    • Click Start, type ISE, and then click Windows PowerShell ISE.
    • From the Start menu, click Start, click All Programs, click Accessories, click the Windows PowerShell folder, and then click Windows PowerShell ISE.

    At the Command Prompt

    In cmd.exe , Windows PowerShell, or Windows PowerShell ISE, to start Windows PowerShell, type:

    With Administrative privileges (Run as administrator)

    Click Start, type ISE, right-click Windows PowerShell ISE, and then click Run as administrator.

    How to Enable Windows PowerShell ISE on Earlier Releases of Windows

    In Windows PowerShell 4.0 and Windows PowerShell 3.0, Windows PowerShell ISE is enabled by default on all versions of Windows. If it isn’t already enabled, Windows Management Framework 4.0 or Windows Management Framework 3.0 enables it.

    In Windows PowerShell 2.0, Windows PowerShell ISE is enabled by default on Windows 7. However, on Windows Server 2008 R2 and Windows Server 2008, it’s an optional feature.

    To enable Windows PowerShell ISE in Windows PowerShell 2.0 on Windows Server 2008 R2 or Windows Server 2008, use the following procedure.

    To enable Windows PowerShell Integrated Scripting Environment (ISE)

    1. Start Server Manager.
    2. Click Features and then click Add Features.
    3. In Select Features, click Windows PowerShell Integrated Scripting Environment (ISE).

    Starting the 32-Bit Version of Windows PowerShell

    When you install Windows PowerShell on a 64-bit computer, Windows PowerShell (x86), a 32-bit version of Windows PowerShell is installed in addition to the 64-bit version. When you run Windows PowerShell, the 64-bit version runs by default.

    However, you might occasionally need to run Windows PowerShell (x86), such as when you’re using a module that requires the 32-bit version or when you’re connecting remotely to a 32-bit computer.

    To start a 32-bit version of Windows PowerShell, use any of the following procedures.

    Читайте также:  Установка amd драйвера linux debian
    Оцените статью