PowerShell stands as a powerful scripting language, allowing users to interact with diverse Windows components. These scripts enable users to modify the Registry, work with the file system, manage group policies, tweak system settings, and much more. If you’re interested in automating tasks on your Windows PC, PowerShell serves as an excellent starting point. In this guide, we provide a fundamental introduction on creating and executing a PowerShell script on both Windows 10 and 11.
Creating a PowerShell Script Using Notepad
- Open Notepad from the Start menu.
- Use the following simple
Start-Process
command to open “azmotech.com” in Chrome.
Start-Process chrome.exe "azmotech.com"
- To create a PowerShell script, click on “File” and choose “Save as.”
- Provide a name for the file and ensure to add “
.ps1
” at the end to designate it as an executable PowerShell script. For instance, name itscript.ps1
and save it on the Desktop.
- To execute the PowerShell script, right-click on the
script.ps1
file and select “Run with PowerShell.“
- That’s it. A Chrome window (or your default browser) will open, launching the specified website. This serves as a basic example of creating and running a PowerShell script.
Running a PowerShell Script from PowerShell or CMD
- Open PowerShell or Terminal from the Start menu. Admin privileges are not required to run a script.
- Execute the following command to allow PowerShell to execute scripts. This command permits PowerShell to run local scripts. If the first command encounters an error, try the second command.
Set-ExecutionPolicy RemoteSigned
or,
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
- Move to the location where you have saved the script using the
cd
command.
cd Desktop
- Run the command in the following format. Start with
.\
and add the filename (without spaces) along with the.ps1
extension. Press Enter.
.\script.ps1
- This will run the PowerShell script from the Terminal window. In this case, a browser window will open and launch the website.
- You can also use a Command Prompt window to run a PowerShell script. Execute the following commands.
cd Desktop
powershell .\script.ps1
Creating and Running Scripts Using PowerShell ISE
Windows 10 and 11 are equipped with PowerShell ISE (Integrated Scripting Environment), providing a user-friendly platform for writing and executing PowerShell code. This environment functions similarly to other Integrated Development Environments (IDEs), supporting code indentation and various modern features. Here’s how you can utilize it:
- Open the Start menu and search for “PowerShell ISE.” Launch the application.
- In the top pane, you can write your PowerShell code, and it will be executed in the bottom pane.
- For a simple demonstration, consider the following code that outputs “Hello, World!” in the PowerShell terminal
Write-Host "Hello, World!"
- Click the “Run” button in the top toolbar to execute the code, and observe the output in the terminal below.
- Here’s another basic example that displays the current date and time.
$dateandtime = Get-Date -Format "dd-MM-yyyy HH:mm:ss"
Write-Host "The current date and time is: $dateandtime"
- To delve deeper into PowerShell scripting, refer to Microsoft’s comprehensive documentation here.
That’s the process for creating and executing a PowerShell script on Windows 11 and 10. PowerShell proves to be a powerful scripting language, providing extensive capabilities to interact with the operating system and streamline tasks through the command line. For further time-saving automation on Windows 10, explore our tutorial on utilizing Task Scheduler and PowerShell commands. Be sure to take a look.
If you’re inclined towards terminal customization, delve into how you can personalize Windows Terminal to impart a fresh appearance. Should you have any lingering questions, feel free to ask in the comments section below.
0 Comments