You must have seen in my previous blog on the commands, operators and variables in Windows PowerShell. If not, you can also refer to the link;
Is it so easy to Create and Run the Scripts in Windows PowerShell??? Well it’s not.
This blog post mainly focuses on the problems that may arouse while Creating or Running the Scripts.
Creation of Scripts:
- How to create directory or file in a PowerShell?
For creating a directory-
PS C:\> new-item -name “new folder” -type directory
For creating a file-
PS C:\> new-item -name “abc.text” -type file
- How to create script in a PowerShell?
As you have learnt from above that how to create directory or file, so now it would be much easy for you to understand the creation of script:
PS C:\> new-item -name “data.ps1” -type file
So, the PowerShell script is being created with the name “data.ps1” where .ps1 is an extension name for PowerShell script.
- How to insert the content in a script?
For the insertion of content in a script file do the following:
Select the file from the location and double-click it
- Write the command that you want in the script.
- Save it.
Running of Scripts:
Just write the following command to run the script:
PS C:\> .\data.ps1
Now what’s that?
The error is “File C:\Users\Piyush\data.ps1 cannot be loaded because the execution of scripts is disabled on this system.”
Let’s check the default Windows PowerShell execution policy:
Here, the default execution policy is Restricted which will prevents all the scripts from running, including scripts that you write in the local computer.
So we have to change the execution policy to RemoteSigned, which will allow the scripts to be run on the PowerShell. For this follow the 2-step carefully:
- Open the “Windows PowerShell Run as Administrator” from the Start menu:
- Change the Execution Policy:
Now the Execution Policy is being set and you can get back to your Windows PowerShell screen and then run the command again:
PS C:\> .\data.ps1
So this is how you should create and run the scripts.
Well this is enough for this post. Hope you like it
In the next blog post I would like to tell you more about this Microsoft’s amazing feature Windows PowerShell….So Stay Tuned!