PowerShell
Scripting
Programming
Tech tutorial
Computer Science

How to run a PowerShell script

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Running a PowerShell script can be a straightforward but powerful tool for automating tasks on Windows, as well as on other platforms with PowerShell Core. Whether you're performing administrative tasks, automating routine procedures, or managing your cloud resources, PowerShell scripting is an invaluable skill.

Understanding Script Execution Policies

Before you begin writing and running PowerShell scripts, it’s important to understand PowerShell’s ExecutionPolicy. This feature helps protect your system by controlling the conditions under which PowerShell loads configuration files and runs scripts. To view your current execution policy, you can use the Get-ExecutionPolicy command. Common policies include:

  • Restricted: Disallows script execution.
  • RemoteSigned: Allows scripts downloaded from the internet to run only if they are signed by a trusted publisher.
  • AllSigned: Allows scripts to run only if they are signed.
  • Unrestricted: Allows all scripts to run.

You can change the policy with the Set-ExecutionPolicy command. For example:

powershell
Set-ExecutionPolicy RemoteSigned

Creating a PowerShell Script

  1. Open a Text Editor: You can use any text editor, such as Notepad or Visual Studio Code.
  2. Write Your Script: A basic script might look something like this:
powershell
    Write-Output "Hello, world!"
  1. Save Your File: Use the .ps1 file extension—for example, HelloWorld.ps1.

Running Your PowerShell Script

To run your script, you'll need to follow these steps:

  1. Open PowerShell: Right-click PowerShell and select "Run as Administrator" to ensure sufficient privileges.
  2. Navigate to the Script’s Directory: Use the cd command to change the directory to where your script is located.
powershell
    cd C:\Path\To\Your\Script
  1. Execute the Script: Run the script by prefixing it with .\ (which represents the current directory in PowerShell):
powershell
    .\HelloWorld.ps1

Security and Best Practices

When running scripts, especially those downloaded from the Internet or received via email, you should always review the code before executing to ensure it doesn’t contain harmful commands. Running scripts as an administrator should be done with caution to avoid unintended changes to system settings or files.

Advanced Scripting Techniques

As you become more comfortable with basic scripting, you might want to explore more advanced features of PowerShell:

  • Functions and Modules: Organize your code into reusable blocks.
  • Error Handling: Use try, catch, and finally blocks for better error management.
  • PowerShell Remoting: Run scripts on remote computers seamlessly.

Useful Commands and Scripts

Some common tasks that can be automated with PowerShell scripts include:

  • File operations (copying, moving, deleting files)
  • Systems administration tasks (checking service statuses, restarting services)
  • Network operations (pinging, port scanning)
  • User administration (creating, modifying users)

Summary Table

CommandPurposeExample
Get-ExecutionPolicyChecks current execution policyGet-ExecutionPolicy
Set-ExecutionPolicySets a new execution policySet-ExecutionPolicy RemoteSigned
Write-OutputPrints output to the consoleWrite-Output "Your message here"
cdChanges directorycd C:\Path\To\Your\Script
Script executionHow to execute a script.\HelloWorld.ps1

Conclusion

PowerShell scripting is essential for automating repetitive tasks, and streamlining system management. By setting the correct execution policies, writing scripts thoughtfully, and carefully executing them, you can substantially increase your productivity and system reliability. As you develop your scripting skills, you'll discover an increasing ability to harness the full power of PowerShell to manage virtually any aspect of Windows-based systems.


Course illustration
Course illustration

All Rights Reserved.