Windows powershell run scripts

Adam the Automator

How to Run a PowerShell Script From the Command Line and More

Parveen Singh

Read more posts by this author.

If you’re new to the PowerShell scripting language and want to learn how to run a PowerShell script, you’ve come to the right blog post. This blog will be a tutorial covering common ways to run scripts and a few issues that may pop up.

Table of Contents

Prerequisites

This article will be a walkthrough for you about how to run PowerShell on your local computer. If you’d like to follow along, please be sure you have the following prerequisites in place before starting this article.

  • A Windows 10 computer with Administrator privileges.
  • Windows PowerShell version 5 or higher. You can also use PowerShell v7. This tutorial will focus on Windows PowerShell since the Windows operating system already has it.
  • Any text file editor

Dealing with the Execution Policy

If this is the first time you’re trying to execute a Windows PowerShell script, you may run into a common problem. PowerShell will probably return an error message stating that a script “cannot be loaded because running scripts is disabled on this system”.

PowerShell returns the error message above when you try to run a PowerShell with an execution policy set to Restricted, Remote Signed or All Signed.

Restricted

Restricted is the default policy set for Windows client computers. If you are using PowerShell for the first time, your default policy would probably be set to restrict all the scripts.

You can still execute individual commands in a terminal, but not a script file. The restriction includes any file ending with .ps1xml , .psm1 or .ps1 .

Unrestricted

Unrestricted allows you to run any script however, it warns you before execution if the script is downloaded from the internet. This policy is usually the default for any non-windows devices.

Remote Signed

Remote Signed policy allows you to run any script that is either (a) digitally signed or (b) any script written on your local computer with or without a signature.

If a script is downloaded from the internet and not signed, you would need to unblock the file. You can do so by right-clicking on the file and choosing Properties. Or, you could use the Unblock-File PowerShell cmdlet for that particular script file.

Using a Remote signed policy would be an ideal option when running a script downloaded from the internet.

All Signed

All signed requires all the scripts to be signed digitally by a trusted publisher. This includes the scripts downloaded from the internet and written locally on your computer.

Читайте также:  Linux caps lock delay

Changing the PowerShell Execution Policy

To change the execution policy:

  1. Open Windows PowerShell with Run as Administrator to make sure you have the highest permission to make the policy changes.

2. When open, run the following PowerShell command to set your computer’s execution policy. The execution policy, as covered above, can be one of three different types. This tutorial is using a useful yet still secure execution policy of RemoteSigned.

Since this tutorial assumes you’ve downloaded from the Internet the GetServices.ps1 script file, set the execution policy to RemoteSigned.

The RemoteSigned execution policy forces you to cryptographically sign every PowerShell script downloaded from the Internet before PowerShell will run it on your system.

3. You should see an output requesting to confirm the action. Enter Y and hit enter to confirm the policy change.

At this point, follow the next steps to explore different methods to run the PowerShell script on your computer.

Running a PowerShell Script

To demonstrate running a PowerShell script, you actually need a script file to run! If you don’t have one handy, download this ZIP file and extract the PS1 file within. You’ll find a simple script file inside called GetServices.ps1.

Every PowerShell script should end with a .ps1 extension.

Using the Console

Once you have a script ready, there are a few different ways you can execute a PowerShell script file. One of the most common ways is via the PowerShell console.

  1. Open the PowerShell console as shown above.

2. Navigate to the file system location your script is located using the Set-Location PowerShell cmdlet or the cd alias. This tutorial’s script is found in the C:\Temp directory.

3. Run the script using a dot ( . ) notation. PowerShell is a shell that also looks for command names. To differentiate between a PowerShell command and a script, you must preface the script with a dot. This dot represents the current directory.

Running a PowerShell Script from the Command Line

If you can’t or would rather not run scripts via the PowerShell console, you can also do so with the good ol’ command line (command prompt).

To run scripts via the command prompt, you must first start up the engine (powershell.exe) and then pass the script path as a parameter to it.

You can run scripts with parameters in any context by simply specifying them while running the PowerShell executable like powershell.exe -Parameter ‘Foo’ -Parameter2 ‘Bar’ .

Once you open cmd.exe, you can execute a PowerShell script like below. This example is running the engine and passing it the script path of C:\Temp\GetServices.ps1.

Notice below that the example below is using the full path to run the script. You’ll have to do this if the folder isn’t in your PATH somewhere.

Below is a handy YouTube video that covers executing a script via a batch file which the cmd.exe executes.

Using the PowerShell ISE

If you create your own scripts or edit others’, you’ll probably be using a script editor like the PowerShell ISE or maybe Visual Studio (VS) Code. Since the ISE comes with Windows, let’s focus on that method for this tutorial.

To invoke a script via the ISE:

  1. Navigate to Start Menu, search for PowerShell ISE and open it.

2. Click on FileOpen and find your script.

3. With the script open, click on the green run button to execute the script. This button will invoke the script in the built-in PowerShell terminal at the bottom.

The Sample Script’s Output

A PowerShell script can sometimes return output. This happens when the script you’re executing is built to return objects which is a fundamental component of PowerShell.

Читайте также:  Не появляются диски при установке windows

If you run the sample GetServices.ps1 script, you will see the following. This script runs the Get-Service cmdlet which returns all of the services installed on your local Windows computer.

Running a PowerShell Script from Within a Script

Let’s say you have two scripts and you’d like one to call the other. Perhaps you have a script called GetUser.ps1 and one called ResetPassword.ps1. Inside of the GetUser.ps1 script, you’d like to execute the ResetPassword.ps1 to reset a user password.

Inside of the calling script (GetUser.ps1), you’d add a line to execute the other script just like you would call the script from the command line.

You can see below you have a couple of options. You should typically choose to run the other script within the same session or scope to simplify things unless you have a specific reason to run the script in another PowerShell session.

How to Write and Run Scripts in the Windows PowerShell ISE

This article describes how to create, edit, run, and save scripts in the Script Pane.

How to create and run scripts

You can open and edit Windows PowerShell files in the Script Pane. Specific file types of interest in Windows PowerShell are script files ( .ps1 ), script data files ( .psd1 ), and script module files ( .psm1 ). These file types are syntax colored in the Script Pane editor. Other common file types you may open in the Script Pane are configuration files ( .ps1xml ), XML files, and text files.

The Windows PowerShell execution policy determines whether you can run scripts and load Windows PowerShell profiles and configuration files. The default execution policy, Restricted, prevents all scripts from running, and prevents loading profiles. To change the execution policy to allow profiles to load and be used, see Set-ExecutionPolicy and about_Signing.

To create a new script file

On the toolbar, click New, or on the File menu, click New. The created file appears in a new file tab under the current PowerShell tab. Remember that the PowerShell tabs are only visible when there are more than one. By default a file of type script ( .ps1 ) is created, but it can be saved with a new name and extension. Multiple script files can be created in the same PowerShell tab.

To open an existing script

On the toolbar, click Open, or on the File menu, click Open. In the Open dialog box, select the file you want to open. The opened file appears in a new tab.

To close a script tab

Click the Close icon (X) of the file tab you want to close or select the File menu and click Close.

If the file has been altered since it was last saved, you’re prompted to save or discard it.

To display the file path

On the file tab, point to the file name. The fully qualified path to the script file appears in a tooltip.

To run a script

On the toolbar, click Run Script, or on the File menu, click Run.

To run a portion of a script

  1. In the Script Pane, select a portion of a script.
  2. On the File menu, click Run Selection, or on the toolbar, click Run Selection.

To stop a running script

There are several ways to stop a running script.

  • Click Stop Operation on the toolbar
  • Press CTRL + BREAK
  • Select the File menu and click Stop Operation.

Pressing CTRL + C also works unless some text is currently selected, in which case CTRL + C maps to the copy function for the selected text.

How to write and edit text in the Script Pane

You can copy, cut, paste, find, and replace text in the Script Pane. You can also undo and redo the last action you just performed. The keyboard shortcuts for these actions are the same shortcuts used for all Windows applications.

Читайте также:  Экранные лупы для windows

To enter text in the Script Pane

  1. Move the cursor to the Script Pane by clicking anywhere in the Script Pane, or by clicking Go to Script Pane in the View menu.
  2. Create a script. Syntax coloring and tab completion provide a richer editing experience in Windows PowerShell ISE.
  3. See How to Use Tab Completion in the Script Pane and Console Pane for details about using the tab completion feature to help in typing.

To find text in the Script Pane

  1. To find text anywhere, press CTRL + F or, on the Edit menu, click Find in Script.
  2. To find text after the cursor, press F3 or, on the Edit menu, click Find Next in Script.
  3. To find text before the cursor, press SHIFT + F3 or, on the Edit menu, click Find Previous in Script.

To find and replace text in the Script Pane

Press CTRL + H or, on the Edit menu, click Replace in Script. Enter the text you want to find and the replacement text, then press ENTER .

To go to a particular line of text in the Script Pane

In the Script Pane, press CTRL + G or, on the Edit menu, click Go to Line.

Enter a line number.

To copy text in the Script Pane

In the Script Pane, select the text that you want to copy.

Press CTRL + C or, on the toolbar, click the Copy icon, or on the Edit menu, click Copy.

To cut text in the Script Pane

  1. In the Script Pane, select the text that you want to cut.
  2. Press CTRL + X or, on the toolbar, click the Cut icon, or on the Edit menu, click Cut.

To paste text into the Script Pane

Press CTRL + V or, on the toolbar, click the Paste icon, or on the Edit menu, click Paste.

To undo an action in the Script Pane

Press CTRL + Z or, on the toolbar, click the Undo icon, or on the Edit menu, click Undo.

To redo an action in the Script Pane

Press CTRL + Y or, on the toolbar, click the Redo icon, or on the Edit menu, click Redo.

How to save a script

An asterisk appears next to the script name to mark a file that hasn’t been saved since it was changed. The asterisk disappears when the file is saved.

To save a script

Press CTRL + S or, on the toolbar, click the Save icon, or on the File menu, click Save.

To save and name a script

  1. On the File menu, click Save As. The Save As dialog box will appear.
  2. In the File name box, enter a name for the file.
  3. In the Save as type box, select a file type. For example, in the Save as type box, select ‘PowerShell Scripts ( *.ps1 )’.
  4. Click Save.

To save a script in ASCII encoding

By default, Windows PowerShell ISE saves new script files ( .ps1 ), script data files ( .psd1 ), and script module files ( .psm1 ) as Unicode (BigEndianUnicode) by default. To save a script in another encoding, such as ASCII (ANSI), use the Save or SaveAs methods on the $psISE.CurrentFile object.

The following command saves a new script as MyScript.ps1 with ASCII encoding.

The following command replaces the current script file with a file with the same name, but with ASCII encoding.

The following command gets the encoding of the current file.

Windows PowerShell ISE supports the following encoding options: ASCII, BigEndianUnicode, Unicode, UTF32, UTF7, UTF8, and Default. The value of the Default option varies with the system.

Windows PowerShell ISE doesn’t change the encoding of script files when you use the Save or Save As commands.

Оцените статью