How to Run a PowerShell Script: Easy Steps 2024

How to Run a PowerShell Script

Table of Contents

PowerShell is a powerful scripting and automation platform used by IT professionals and administrators worldwide. Whether you’re automating tasks, managing systems, or building complex deployment scripts, PowerShell provides the flexibility and control needed to handle a variety of challenges. This guide will cover everything you need to know about how to run a PowerShell script, from the basics to more advanced techniques.

What is PowerShell?

Before diving into how to run PowerShell scripts, it’s important to understand what PowerShell is. It’s a cross-platform (Windows, Linux, and macOS) task automation and configuration management framework. PowerShell helps you automate repetitive tasks and manage your systems more efficiently by using PowerShell scripts, which are written in the PowerShell scripting language.

Basic Steps to Run a PowerShell Script

Enabling Script Execution

By default, PowerShell restricts script execution for security reasons. To run a PowerShell script, you first need to ensure that your system allows it. You can do this by changing the execution policy. Open PowerShell as an administrator and run:

powershellCopy codeSet-ExecutionPolicy RemoteSigned

This command allows you to execute PowerShell scripts that you’ve written or that have been downloaded from the Internet, provided they are signed by a trusted publisher.

Writing Your First Script

To create a PowerShell script, you can simply write commands in a text file and save it with a .ps1 extension. Here’s a basic example:

  1. Open Notepad or another text editor.
  2. Type Write-Output 'Hello, PowerShell!'.
  3. Save the file as Hello.ps1.

Executing the Script

To execute your PowerShell script, navigate to the directory where your script is located in the PowerShell window and type:

powershellCopy code.\Hello.ps1

This command tells PowerShell to execute the script in the current directory.

Running Scripts From Different Sources

Run PowerShell Script from PowerShell

As shown above, you can run scripts in PowerShell directly from the command line interface of PowerShell itself by navigating to the script’s directory and using the .\ operator.

Run PowerShell Script from CMD

You can also run a PowerShell script from CMD (Command Prompt). To do this, open CMD and type:

cmdCopy codepowershell -ExecutionPolicy ByPass -File "C:\Path\To\Script.ps1"

This method is particularly useful if you need to integrate PowerShell scripting into batch processes that are run from CMD.

Execute PowerShell Script from Command Line

For those using Linux or macOS, you can execute a PowerShell script from the command line by opening the terminal and using a similar syntax to what you would use in CMD:

bashCopy codepwsh -File /path/to/script.ps1

Advanced Script Execution Techniques

PowerShell Execute Script with Parameters

To make your scripts more dynamic and adaptable, you can pass parameters to them. Modify your script to accept parameters like this:

powershellCopy codeparam (
    [string]$name
)
Write-Output "Hello, $name!"

You can then execute the PowerShell script with a parameter from the command line:

powershellCopy code.\Hello.ps1 -name "World"

Running PowerShell Scripts in a Remote Session

PowerShell allows you to run scripts on PowerShell remotely, which is extremely useful for managing multiple machines. Use the Invoke-Command cmdlet like so:

powershellCopy codeInvoke-Command -ComputerName RemotePC -ScriptBlock {
    param($name)
    Write-Output "Hello, $name from RemotePC!"
} -ArgumentList "World"

This capability is invaluable for network administrators and those managing multiple systems across different locations.

Conclusion

Running a PowerShell script can seem daunting at first, but with a bit of practice, it becomes a straightforward task. By following this guide, you can execute PowerShell scripts with ease, enhancing your productivity and extending your administrative capabilities. Whether you’re running scripts in PowerShell locally, from CMD, or across networks, the flexibility of PowerShell makes it an indispensable tool for any tech professional’s toolkit.

You may find some of our other articles interesting

How to Convert a List to String in Python: All you need to know with examples
Create SQL Temp Table: Mastering Temporary Tables in SQL
fatal: need to specify how to reconcile divergent branches – Full Guide for 2024
Transforming Development Processes: 6 Ways ChatGPT Can Help Developers 

Join our newsletter to stay updated

Sydney Based Software Solutions Professional who is crafting exceptional systems and applications to solve a diverse range of problems for the past 10 years.

Share the Post

Related Posts

Join our newsletter to stay updated