We earn commission when you buy through affiliate links.
This does not influence our reviews or recommendations.Learn more.
Working with PowerShell script can be an exciting prospect.
After all, it gives you a clean interface to create your scripts for automating daily tasks.
This article looks closer at creating and running PowerShell scripts.
What is PowerShell?
PowerShellis a cross-platform automation and configuration tool.
Technically, PowerShell is built on top of the .NET framework.
Due to his approach, it supports Objects and structured data such as JSON, CSV, and XML.
Developers can also use REST APIs with PowerShell.
Unfortunately, the basic Command Prompt doesnt support any of it as it is text-based.
All PowerShell scripts are written in the .ps1extension.
Overall, PowerShell is an excellent tool for scripting.
However, Windows prevents scripts from running due to security tweaks.
This can lead to errors such as running scripts is disabled on this system.
To bypass it, youll need to set the correct execution policy.
(covered later in the article.)
Why should you use PowerShell for scripting?
One of the biggest reasons PowerShell is an excellent pick for scripting is its ability to work across platforms.
It supports Windows, Linux, and MacOS, which the default Windows Command Prompt doesnt.
you’ve got the option to use PowerShell to create simple jobs using its easy task builders.
Additionally, PowerShell scripting is mainly used for system management automation.
How are PowerShell Scripts created?
To create PowerShell Scripts, youll need a text editor.
(PowerShell ISE).
#1.With VS Code
VS Code is a robust and reliable free cross-platform code editor.
Microsoft develops it, giving you the added benefit of good PowerShell support.
You get a clean interface out of the box and can use it with virtually anyprogramming language.
As VS Code is a proper code editor, it supports extensions.
And to improve our PowerShell scripting experience, we will use the PowerShell extension.
But, before all of these, you should probably install VS Code.
As were using Windows 11, we went with the Windows version.
Once downloaded, double-pick the installer, and follow the wizard to install it on your system.
you might plant the PowerShell extension in two ways.
First, create a new file with the.ps1extension.
it’s possible for you to select Install to get it installed.
Otherwise, click Show recommendation to learn more about it.
Now, search for PowerShell from Microsoft and choose the first result.
From there, proceed to Install it.
Finally, itll ask you if you want to trust the authors of the files in the workspace.
Press Trust Workspace & Install to complete the installation.
you’re able to now create your first Powershell script with VS Code and its extensions properly installed.
you could now create your script accordingly.
I added a Write-Host cmdlet with a value of This is a VS Code Script written for Geekflare.
Well see how to execute the script later, so keep reading!
#2.With Notepad
Creating PowerShell scripts in Notepad is easy.
All it’s crucial that you do is follow the steps below:
1.
Open Start and search for Notepad.
Now, right-tap on it and select Open as an administrator.
Now, write your script.
For the tutorial, we will write, This is a PowerShell Script written in Notepad.
Go to File at the top menu and click Save as.
Select your PowerShell scripts name and click Save.
You have successfully created a PowerShell script with Notepad.
#3.With PowerShell ISE
PowerShell on Windows also offers its own Integrated Scripting Environment.
it’s possible for you to use it to code your scripts without relying on third-partycode or text editors.
To use it successfully, youll need to use it through the steps mentioned below:
1.
Itll launch the PowerShell ISE window.
Now, choose File and then select New.
Itll open a new file Window as below.
- punch in your desired script.
For the tutorial, well write, This is a PowerShell Script written with the help of PowerShell ISE.
Once done, go to File and Save As.
Here, pick the file name and hit Save.
it’s possible for you to also spin up the script directly from the PowerShell ISE.
To do so, tap the RUN button on the top section of the environment.
If it throws a Security Error, read below how to solve it.
To bypass it, youll need to enable script execution on your Windows machine.
For PowerShell ISE, it looks as below.
For Visual Studio Code, itll look as below.
And, for Notepad, youll need to run it directly from the PowerShell command prompt.
These practices include:
Conclusion
Thats it!
you might now create and run PowerShell scripts using Notepad, VS Code, and PowerShell ISE.