How To Create A Powershell Script In Notepad

In this tutorial, we will learn how to create a PowerShell script using Notepad as the text editor. PowerShell is an automation platform and scripting language for Windows and Windows Server that includes a command-line shell and a scripting language. It is commonly used for system administration tasks like managing services, processes, or users.

Creating a PowerShell script in Notepad allows you to save your code as a .ps1 file which can then be executed as a script. This is a quick and easy way to create a script without requiring any additional tools or software.

Step 1: Open Notepad

First, open Notepad on your Windows computer. You can do this by searching for Notepad in the Start menu or by pressing Windows + R keys and typing “notepad” in the Run window.

Step 2: Write your PowerShell script

Now it’s time to write your PowerShell script. Start with the following line at the beginning of your script:

This is a comment line that won’t be executed. It just helps you identify this file as a PowerShell script.

Next, write your desired PowerShell commands in Notepad. For example, let’s create a script that retrieves the list of running processes and sorts them by their memory usage:

This command gets the list of running processes with Get-Process and sorts them using Sort-Object based on their memory usage (WS or Working Set) in descending order.

Step 3: Save the script

To save your PowerShell script in Notepad, go to File > Save As… In the Save As window, choose a location to save the file and enter a file name followed by the .ps1 extension (e.g., MyScript.ps1). Make sure to set the “Save as type” dropdown to All Files (*.*) to avoid appending an additional .txt extension to the file.

Click on the Save button to save your PowerShell script.

Step 4: Execute the script

To execute the script, open PowerShell by searching for it in the Start menu. Before running the script, you may need to change the execution policy to allow running unsigned scripts. Run the following command:

Note: Changing the execution policy can potentially expose your computer to security threats. Please make sure to understand the risks before changing the policy. More information can be found here.

To run your script, navigate to the directory where your script is saved and type ./MyScript.ps1 (replacing MyScript.ps1 with the name of your script) and press Enter. The script will execute, and the output will be displayed in the PowerShell window.

Output

Step 5: Modify the script

If you want to modify the script later, simply open the .ps1 file in Notepad again, make the necessary changes, and save the file. Rerun the script in PowerShell to see the updated output.

Full code

Conclusion

Creating a PowerShell script using Notepad is a simple and convenient method for writing and executing basic scripts. It is a useful skill for system administrators, developers, or anyone looking to automate tasks in Windows environments. Remember to understand the risks involved in executing scripts and only run scripts you trust or have written yourself.