How to Run PowerShell Script

Microsoft has developed a powerful scripting language called PowerShell. It is mainly used for automating administrative tasks, managing system configurations and handling complex operations on windows operating system. PowerShell scripts can significantly enhance efficiency and allow users to automate repetitive tasks, manage system settings and integrate different applications.
If you are a windows users or system administrator, it will definitely help you if you learn how to run PowerShell scripts. In this guide, we’ll explore different ways to execute PowerShell scripts safely and effectively.

What is a PowerShell Script?

A PowerShell script is simply a text file which contains a series of PowerShell commands. Instead of running each command manually, you can write them in a script file and execute them all at once.

File Extensions Used for PowerShell Scripts

PowerShell scripts typically use the .ps1 file extension. Some other related file types include:

  • .psm1 – PowerShell module files
  • .psd1 – PowerShell module manifest files
  • .psc1 – PowerShell console files

Common Use Cases of PowerShell Scripts

  • Automating system tasks
  • Managing Active Directory
  • Monitoring system performance
  • Deploying software updates
  • Configuring network settings

Before running a script, you need to ensure your system is ready.
Run the following command to check your PowerShell version:

$PSVersionTable.PSVersion

Opening PowerShell with Administrator Privileges

To open PowerShell as an administrator:

  1. Press Win + X and select Windows Terminal (Admin) or PowerShell (Admin).
  2. Click “Yes” on the User Account Control (UAC) prompt.

Understanding Execution Policies

By default, Windows restricts script execution for security reasons. Running scripts may require modifying the execution policy.

Learn Also Elevated Command Prompt in Windows

Changing PowerShell Execution Policy

Execution policies prevent the execution of unsigned scripts to protect against malicious code. There are 3 different types of execution policies –

  • Restricted – No scripts allowed
  • RemoteSigned – Locally created scripts allowed; downloaded scripts need a digital signature
  • Unrestricted – All scripts allowed (not recommended)

How to Change Execution Policy

You can set execution policy as per your need

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

You can bypass the policy for a single script as mentioned below

powershell -ExecutionPolicy Bypass -File "C:\Path\To\Script.ps1"

How to run PowerShell script

Running a Script from PowerShell Console

To run a script, navigate to its location and use:

.\myscript.ps1

If the script is located elsewhere, use the full path:

C:\Scripts\myscript.ps1
Running a Script from Windows Explorer
  1. Locate the script in File Explorer.
  2. Right-click and choose Run with PowerShell.
Running a Script with Parameters

If a script requires parameters, pass them like this:

.\myscript.ps1 -Parameter1 Value1 -Parameter2 Value2
Running PowerShell Scripts with Task Scheduler

PowerShell scripts can be automated using Task Scheduler to run at specific times or events. This is useful for system maintenance tasks, backups, or monitoring.

Setting Up Task Scheduler

  1. Open Task Scheduler by searching for it in the Windows Start menu.
  2. Click Create Basic Task on the right panel.
  1. Provide a name and description for your task.
  2. Select the trigger (e.g., daily, at system startup, or when a user logs in).
  1. In the Action step, choose Start a Program and enter:

powershell.exe -File “C:\Path\To\YourScript.ps1”

  1. Click Finish to save the task.

Troubleshooting Scheduled Scripts

  • If a script does not run, check the History tab in Task Scheduler.
  • Ensure the script path is correct and that the execution policy allows script execution.
  • If running the script as an admin, configure the task to Run with highest privileges.
Running PowerShell Scripts from Command Prompt

You can run PowerShell scripts from the traditional Command Prompt (CMD) using different methods.

Syntax for Running Scripts from CMD

Open CMD and use:

powershell -File "C:\Path\To\Script.ps1"

Using powershell.exe and pwsh.exe

  • powershell.exe runs Windows PowerShell (older versions).
  • pwsh.exe runs PowerShell Core (cross-platform version).

Example Commands

powershell -NoProfile -ExecutionPolicy Bypass -File "C:\MyScript.ps1"

Handling PowerShell Script Errors

Errors can occur due to syntax mistakes, incorrect permissions, or missing dependencies.

Common Errors and Their Causes

ErrorCauseSolution
Script is not digitally signedExecution policy restrictionsChange policy to RemoteSigned
Access DeniedLack of admin privilegesRun PowerShell as admin
File not foundIncorrect pathVerify script location
Command not recognizedMissing module or cmdletInstall required module

Security Best Practices for Running PowerShell Scripts

PowerShell can be a security risk if not managed properly.

Avoiding Malicious Scripts

  • Never run scripts from untrusted sources.
  • Use Get-AuthenticodeSignature to check script signatures.

Sign scripts using a digital certificate:

Set-AuthenticodeSignature -FilePath "C:\MyScript.ps1" -Certificate (Get-Item Cert:\CurrentUser\My\ABC123)

Managing Permissions for Safe Execution

  • Use Get-ACL and Set-ACL to control script access.
  • Restrict execution policies for standard users.

PowerShell Integrated Development Environment (IDE) Options

Using Windows PowerShell ISE

  • Built-in script editor with debugging tools.
  • Type powershell_ise in Run (Win + R) to launch.

Using VS Code for PowerShell Scripting

  • Install PowerShell extension for VS Code.
  • Use built-in debugging features.

Conclusion

PowerShell scripts efficiently save time, improve automation and enhance system management. By understanding execution policies, troubleshooting errors, and following security best practices, you can safely execute scripts across different platforms.

If you’re new to PowerShell, start with small scripts and gradually move to advanced automation. Happy scripting!


FAQs

1. How do I enable PowerShell script execution?
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

2. How do I run a script without changing execution policy?
powershell -ExecutionPolicy Bypass -File “script.ps1”

3. Can I schedule a PowerShell script to run automatically?
Yes, use Task Scheduler or Register-ScheduledTask in PowerShell.

4. How do I debug a PowerShell script?
Use Write-Host, -Verbose, or Try-Catch blocks.

5. Can I run PowerShell scripts on Linux?
Yes, use PowerShell Core (pwsh).

Leave a Reply

Your email address will not be published. Required fields are marked *