How to Run Scripts in PowerShell: A Complete Step-by-Step Guide

Last update: 18/03/2025
Author Isaac
  • PowerShell requires setting execution policies to allow scripts.
  • Scripts are run with specific paths and optional parameters.
  • Windows PowerShell ISE and VS Code are great for script editing.
  • Task Scheduler allows for the automation of scripts in Windows.

Run scripts in PowerShell

PowerShell is a powerful tool that allows you to automate tasks in Windows environments using scripts. Although it's an essential utility for system administrators, its script execution is restricted by default for security reasons. To properly use PowerShell scripts, it's necessary to understand the execution policies, required permissions, and the different ways to run them.

In this article, we will cover in detail everything you need to know about running scripts in PowerShell, from how to write them to the best way to run them on your system, including solutions for common errors and advanced options such as running on other computers or using Task Scheduler.

What is a script in PowerShell?

Un script In PowerShell it is simply a text file containing one or more lines of commands that are executed sequentially. These files have the extension . Ps1 and can include simple commands or complex blocks of code with control structures, functions and parameters.

Using scripts allows you to run repetitive tasks automatically, share configurations, and manage systems more efficiently. To create them, you can use any text editor, although PowerShell ISE and Visual Studio Code are more recommended options due to their advanced features.

Configuring the execution policy

PowerShell uses an execution policy to determine which scripts can run and under what conditions. The available options are:

  • restricted: No scripts are allowed to run. Only scripts can be run commands in the interactive session.
  • AllSigned: Only digitally signed scripts can be executed with a trusted certificate.
  • RemoteSigned: Scripts downloaded from the Internet must be signed, but local scripts can be executed. unsigned.
  • Unrestricted: All scripts can be executed no restrictions.
  How to use QTTabBar to have tabs and dual view in Explorer

To check the current execution policy, you must run the following command in PowerShell:

Get-ExecutionPolicy

If you want to change it, use the following command with administrator permissions:

Set-ExecutionPolicy RemoteSigned

How to run a script in PowerShell

To run a script in PowerShell, you need to specify the full path of the file. If the script is in the current folder, use:

.
ombre-del-script.ps1

If the script is in another location, specify it in full:

C:\Scripts\nombre-del-script.ps1

You can also run scripts with parameters, as in this example:

.
ombre-del-script.ps1 -Parametro valor

Writing and editing scripts in PowerShell

An efficient way to write and edit scripts in PowerShell is to use PowerShellISE o Visual Studio Code. These environments offer tools such as syntax highlighting, auto-completion and integrated execution.

To open PowerShell ISE:

  • Press Windows + R and writes powershell_ise.
  • Select “Run as administrator” if necessary.

From PowerShell ISE, you can write a script in a new tab and save it with the extension . Ps1. You can then run it directly from this environment.

How to run scripts on other computers

PowerShell allows you to run scripts on other computers remotely using the cmdlet Invoke-CommandTo do this, the remote computer must allow remote sessions.

Example of remote execution on other computers:

Invoke-Command -ComputerName Servidor01 -FilePath C:\Scripts\nombre-del-script.ps1

PowerShell Remoting must be enabled on the target computer. To activate it, run the following command on the remote computer:

Enable-PSRemoting -Force

Automate execution with Task Scheduler

Running PowerShell scripts automatically is a common task in enterprise environments. To do this, you can use the Windows Task Scheduler.

Steps to create a scheduled task that runs a PowerShell script:

  1. Open Task Scheduler (taskschd.msc).
  2. Select “Create a basic task.”
  3. Set the trigger to define when it will run.
  4. Under actions, select “Start a program” and enter powershell.exe.
  5. In parameters, add: -File "C:\ruta-del-script\script.ps1".
  6. Save the task and test it manually.

Automate scripts in PowerShell

Solution to common errors

When running scripts in PowerShell, errors may arise due to execution policy restrictions or insufficient permissions. Some common solutions include:

  • Run PowerShell as administrator: Right-click PowerShell and select “Run as administrator.”
  • Check the execution policy: Use the command Get-ExecutionPolicy and change the settings if necessary.
  • Use absolute paths: Make sure to specify the full path to the script.
  • Allow remote execution: If the script is running on another computer, enable PowerShell Remoting.
  Complete guide to setting up kiosk mode in Windows 11: advanced options, requirements, and tips

Mastering script execution in PowerShell allows you to automate tasks, improve system management, and simplify administrative processes. Properly adjusting execution policies, using appropriate tools to write scripts, and configuring them in Task Scheduler helps increase efficiency and security in Windows environments.

How to Manage Files with Powershell
Related article:
How to Manage Files with Powershell