Powershell Scripting TutorialIntroduction Tools required for Powershell First Powershell script Interacting with userSpecial characters in Windows Powershell scriptingArrays String manipulation Conditional statements Looping statements Passing arguments Passing named arguments Exit status of the script Writing functions
First program in windows powershell scriptingIn this topic, you will learn how to create a simple a Powershell script and execute it. Remember that script is nothing but list of commands. The script has an extension as .ps1 You can add the comments in script by prefixing the line with # symbol. Create a simple file with name “s1.ps1” with below command in it.
Now to execute this script, you need to fire below command from the command prompt.
You may get below error saying “script can not be loaded because running scripts is disabled” if execution policy is not set properly. By default, Powershell has a “Restricted” execution policy. So you are not allowed to run any “.ps1” script in it. There are 4 Execution Policies in Powershell.
- Restricted : This policy does not allow you to run any script in Powershell.
- AllSigned : This policy allows you to run only signed scripts from trusted publishers. You will be asked for confirmation before running script.
- RemoteSigned : This policy lets you run local unsigned scripts. You will not be asked for confirmation before running script.
- Unrestricted : This policy let you execute local as well as remote unsigned scripts. You will not be asked for confirmation before running script.
You can change the current execution policy using below syntax.
To run our script, we will be setting our policy to “RemoteSigned”. Note that you should start the Powershell as an Administrator to execute below command.
After that, you can run your Powershell script using below syntax. You should see the list of directories in output.
Web development and Automation testing