Scripts or Script language is a computer programming language that is used for the execution of various tasks. Every digital app, program, function, or feature we use in our daily life are the result of such scripts. One such Script tool we are going to discuss today is the ‘PowerShell’.
PowerShell is a shell scripting language from Microsoft that is used for configuration management and task automation framework. Initially known as Windows PowerShell it was later made cross-platform software and open-source software and was further introduced as PowerShell Core on 18 August 2016. The latest version is built on .NET Core.
Today, in this PowerShell script beginners guide we will take you through the easiest tutorials that will help you in getting started with PowerShell Scripts. Let’s learn how:
- What is Windows PowerShell?
- Advantages of PowerShell over Command Prompt
- PowerShell extension policies in Windows 10
- Create PowerShell script file in Windows 10
- Learn how to create a script with Visual Studio Code
- Steps to create PowerShell scripts with the Integrated Scripting Environment (ISE)
- Steps to create PowerShell scripts with Notepad
- How to run PowerShell script file on Windows 10
What is Windows PowerShell?
The Windows PowerShell is somewhere a little similar to Command Prompt and is available on Windows, macOS, and Linux. However, unlike Command Prompt, PowerShell is one highly capable Command-line interface (CLI). It offers a large set of tools and comes with great flexibility and control. It comes with an Integrated Scripting Environment (ISE), and that ISE screen is divided into two parts. Where the top part is used for writing scripts, the bottom one is used for running commands (manual).
Advantages of PowerShell over Command Prompt
PowerShell is much more than Command Prompt, and it is explained well through the points below:
Command Prompt uses commands whereas PowerShell uses cmdlets: ‘Cmdlet’s are far more useful than commands used in ‘cmd.exe.’ The Cmdlets is a lightweight command that exposes complex system administration functionalities like Windows Management Instrumentation (WMI) and registry management.
PowerShell is developed using the .NET framework: The .NET framework in PowerShell allows its scripts to use .NET interfaces and other extended features which else way are not provided through cmdlets.
PowerShell is an object-oriented programming language: The data output from cmdlets are not just texts but objects. It provides greater flexibility to play around with complex data.
PowerShell is a task-oriented scripting language: It is a task-based scripting language that provides supports for command-line tools and existing scripts.
PowerShell has an extensible interface: This shell script is customized as independent software vendors. Using PowerShell to administrate their software, enterprise developers can build custom tools and utilities.
PowerShell is very simple to use: While using PowerShell, the command-based simplified navigation system allows the users to easily navigate the registry and other data stores.
PowerShell extension policies in Windows 10
- Restricted: It stops any script from running.
- Unrestricted: It lets runs any script without any restrictions.
- Remote Signed: It only allows scripts created on the device. The scripts created on another system would require a trusted publisher’s signature for running.
- All Signed: It allows all type of scripts to run, with a condition if a trusted publisher has signed them.
Create PowerShell script file in Windows 10
Creating PowerShell scripts in Windows 10 is not a very complicated task. You can either use the Visual Studio Code editor with the PowerShell extension or do it virtually by using any text editor or the ISE console.
Learn how to create a script with Visual Studio Code
VS Code or Visual Studio Code is Microsoft’s free and extensible source code editor. With the VS code, one can easily edit any programming language. Moreover, when adding with the PowerShell extension, it provides the user with an IntelliSense support and a highly interactive scripting editing experience.
Now let’s get started with the learning process:
Steps for installing the VS Code or Visual Studio Code
- Go to the official Webpage of Visual Studio Code.
- Now Click on the Windows download button.
- Once the File is downloaded, double-click on it to start the Virtual Studio Code installation process.
- Here follow the on-screen instructions (confirm the agreement terms).
- After that, click on the Next button, thrice in continuity after every page.
- Again, follow the other on-screen instructions and process as per requirement.
- Now click on Next -> Install -> Finish.
After the process is complete, you can move to next step to install the PowerShell extension.
Steps to Install PowerShell extension
- Firstly, open the Visual Studio Code, then from the left pane locate and select the Extensions tab.
- Now again on the left pane, select and install the topmost PowerShell The installation process may take a while, wait patiently.
- Hereafter successfully installing the Visual Studio Code and PowerShell extension we can now start with creating the script.
Steps to create PowerShell scripts with Visual Studio Code
- Open the Visual Studio code window.
- On the top-left corner, go to File and select the option New File.
- Now save the New File by- go to the File menu and select the option Save as.
- Specify a file name followed with the .ps1 extension and click on Save.
- Now write a new command, for example:
Write-Host “Hello World”
- For running the script, click on the Run button (an arrow-like button on the top-right corner) or press the F5 key.
This script will present the output as, “Hello World”. (You can use any other text in place of Hello World).
- To save this script, go to the File menu and click on the option Save.
Steps to create PowerShell scripts with the Integrated Scripting Environment (ISE)
- On the Windows search bar type Windows PowerShell ISE.
- Right-click on the search result and select the option Run as administrator.
- Go to the File menu on the top left and select New for creating an empty .ps1file.
- Write a new command, for example:
Write-Host “Hello World”
- Now on the top-left corner, go to the File menu and then click on the option Save.
- Save the script with a new name followed by the .ps1extension.
- Finally, click on the Save (You can also change the default folder location to save the script).
Steps to create PowerShell scripts with Notepad
- Go to the Start menu and type and open Notepad.
- Write a new command, as mentioned in the steps above.
- Now on the top-left corner, go to the File menu and select the option Save As.
- Save the script with a new name followed by the .ps1 extension and then click on the Save
Once you are done with writing the scripts successfully, both the concepts except Notepad will be ready to run the script. The script in Notepad will fail by default as the PowerShell settings (default) is generally set to block the execution of any script.
How to run PowerShell script file on Windows 10
For successfully running a script with PowerShell, one can change the execution policy on Windows 10. Follow the steps given below:
- Go to the Search bar in your desktop and type Windows PowerShell.
- On the search result right-click and select Run as administrator option.
- Type the following command, and then press Enter to allow scripts to run.
- Now type A and then again press Enter.
- Then type the following command and then press Enter.
Note: In place of PATH\TO\SCRIPT, make sure to mention the location of your script. For example, Users\username\Downloads.
- Once you are done, the PowerShell script will run successfully.
By the end of this article, we hope that we have successfully guided you with “How to get started with PowerShell Scripts”. The PowerShell Scripts is a huge topic and offer you several resources and techniques to learn about.
We have tried to cover all the beginners essential with the easiest explanation above. The goal of this article is to guide our readers with how to get started using PowerShell and put together some simple scripts. If you have any queries or feedback, please write down the comment in the below comment box.