How to Create and Run a PowerShell Script on Windows 10 and 11

In Short
  • PowerShell is a scripting language for Windows and is used for OS configuration and automation. You can use Notepad or PowerShell ISE to test your scripts.
  • You can also run PowerShell scripts from a Terminal window, be it PowerShell itself or Command Prompt.
  • Before running the script, you need to change the execution policy to "RemoteSigned" to run a local PowerShell script.

PowerShell is an excellent scripting language that lets you interact with various Windows components. With PowerShell scripts, you can modify and work with the Registry, file system, group policies, system settings, and more. If you are someone who wishes to automate tasks on their Windows PC, PowerShell is the best place to start your journey. So, in this guide, we give you a basic primer on how to create and run a PowerShell script on Windows 10 and 11.

Create a PowerShell Script Using Notepad

  • Open Notepad from the Start menu.
  • Now, we are going to use a simple Start-Process command to open “beebom.com” in Chrome.
Start-Process chrome.exe "beebom.com"
launch a process using powershell command in notepad
  • To create a PowerShell script, click on “File” and choose “Save as“.
save as file option in notepad
  • Give a name to the file and make sure to add “.ps1 at the end to make it an executable PowerShell script. I have named it script.ps1 and saved the file on the Desktop.
save a file as powershell script (.ps1) on windows
  • To run the PowerShell script, right-click on the script.ps1 file and choose “Run with PowerShell“.
run powershell script from windows 11 context menu
  • That’s it. A Chrome window (or whatever your default browser is) will open up and launch the website. This is a basic example of how to create a PowerShell script and run it.
windows 11 desktop interface with beebom website launched in chrome

Run a PowerShell Script from PowerShell or CMD

  • You can also run the script from a PowerShell terminal. Open PowerShell or Terminal from the Start menu. You don’t need the admin privilege to run a script.
  • Now, run the below command to allow PowerShell to execute scripts. This will let PowerShell run local scripts. If the first command shows an error, run the second command.
Set-ExecutionPolicy RemoteSigned

or,

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
change execution policy in powershell to allow local scripts
  • Next, move to the location where you have saved the script using the cd command.
cd Desktop
  • After that, run the command in the below fashion. Start with “.\ and add the filename (without a space) along with the .ps1 extension. Now, hit Enter.
.\script.ps1
run a powershell script from powershell command line
  • This will execute the PowerShell script from the Terminal window. In this case, a browser window will open up and launch the website.
  • You can also fire up a Command Prompt window to run a PowerShell script. Execute the command in the below fashion.
cd Desktop
powershell .\script.ps1
run powershell script from cmd

Create and Run Scripts Using PowerShell ISE

Windows 10 and 11 come with a PowerShell ISE (Integrated Scripting Environment) that allows you to write PowerShell code and run it in an integrated environment seamlessly. It works like any other IDE and supports code indentation and many other modern features. Here is how you can use it.

  • Open the Start menu and search for “PowerShell ISE“. Now, open it.
open powershell ise from windows 11 start menu
  • Next, in the top pane, you can write the code, and it will be executed at the bottom pane.
powershell ISE interface
  • To demonstrate a basic example, I have written the below code which outputs “Hello, World!” in the PowerShell terminal.
Write-Host "Hello, World!"
  • To run the code, simply click on the “Run” button in the top toolbar. You can see the output in the Terminal below.
sample program to run in powershell ISE
  • Here is another basic example. Run it and this will show the current date and time.
$dateandtime = Get-Date -Format "dd-MM-yyyy HH:mm:ss"
Write-Host "The current date and time is: $dateandtime"
find current date and time using powershell ISE scripting
  • To learn more about PowerShell scripting, you can head over to Microsoft’s excellent documentation here.

So, this is how can make and run a PowerShell script on Windows 11 and 10. PowerShell is a great scripting language for Microsoft to interact with the operating system and get things done from the command line. We have already written a handy tutorial on how to automate tasks on Windows 10 to save time using Task Scheduler and PowerShell commands. Make sure to check it out.

Apart from that, if you are a Terminal junkie, learn how to customize Windows Terminal to give a fresh look. Anyway, that is all from us. If you still have any questions, let us know in the comment section below.

Comments 0
Leave a Reply

Loading comments...