How to run a PowerShell script

Learn how to run a powershell script with practical examples, diagrams, and best practices. Covers windows, powershell, scripting development techniques with visual explanations.

Mastering PowerShell Script Execution: A Comprehensive Guide

Hero image for How to run a PowerShell script

Learn the essential methods for running PowerShell scripts, from basic execution to advanced techniques, ensuring secure and efficient automation on Windows.

PowerShell is a powerful scripting language and command-line shell developed by Microsoft, primarily used for automating administrative tasks and managing Windows systems. Running PowerShell scripts is a fundamental skill for IT professionals and developers alike. This article will guide you through various methods of executing PowerShell scripts, covering common scenarios and best practices for security and efficiency.

Understanding Execution Policies

Before you can run any PowerShell script, it's crucial to understand PowerShell's execution policies. These security features control the conditions under which PowerShell loads configuration files and runs scripts. By default, the execution policy is often set to Restricted, which prevents any scripts from running. You'll need to adjust this policy to allow script execution.

flowchart TD
    A[Start] --> B{"Is Execution Policy 'Restricted'?"}
    B -->|Yes| C[Change Execution Policy]
    C --> D[Run Script]
    B -->|No| D[Run Script]
    D --> E[End]

Flowchart illustrating the decision process for PowerShell execution policies.

Checking and Changing Execution Policy

You can check your current execution policy using the Get-ExecutionPolicy cmdlet. To change it, use Set-ExecutionPolicy. Common policies include Restricted, AllSigned, RemoteSigned, and Unrestricted. For most development and administrative tasks, RemoteSigned is a suitable choice.

Get-ExecutionPolicy
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Checking and setting the PowerShell execution policy for the current user.

Methods for Running PowerShell Scripts

Once your execution policy is configured, you have several ways to run a PowerShell script. The method you choose often depends on the context, whether you're running it interactively, from a scheduled task, or as part of a larger automation workflow.

1. From the PowerShell Console

This is the most common method for interactive execution. Navigate to the directory where your script is located using cd, then type the script's full path or relative path, prefixed with .\ for local scripts. For example, if your script is named MyScript.ps1 in the current directory, you would type .\MyScript.ps1.

2. Using the powershell.exe Command-Line Tool

You can execute scripts directly from cmd.exe or the Run dialog (Win+R) using the powershell.exe command. The -File parameter is used to specify the script path. This is useful for batch files or launching scripts from other applications. Example: powershell.exe -File "C:\Scripts\MyScript.ps1".

3. From PowerShell ISE or VS Code

Integrated Scripting Environments (ISEs) like the built-in PowerShell ISE or Visual Studio Code with the PowerShell extension provide a rich environment for writing, debugging, and running scripts. You can open the script file and click the 'Run' button (often a green triangle) or press F5.

4. Scheduling with Task Scheduler

For automated, recurring tasks, Windows Task Scheduler is ideal. Configure a new task to run powershell.exe with the -File parameter pointing to your script. Ensure the task runs with appropriate user credentials and execution policy settings.

# Example: Running a script from the console
cd C:\MyScripts
.\MyScript.ps1 -Parameter1 "Value"

# Example: Running a script from cmd.exe or Run dialog
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\MyScript.ps1" -LogPath "C:\Logs"

# Example: Running a script and passing arguments
$scriptPath = "C:\Scripts\MyScript.ps1"
$arg1 = "Hello"
$arg2 = "World"
& $scriptPath -Param1 $arg1 -Param2 $arg2

Various ways to execute PowerShell scripts, including passing parameters.