There are a few different ways you can execute a PowerShell script. In this tutorial, I’m going to show you the most popular methods.
Let’s create a very simple script and name it script.ps1.
This code will display the “Hello, World!” message.
- Click the magnifying glass on the Windows taskbar and type “Powershell”.
- Right-click Windows PowerShell and choose Run as Administrator.
Enable execution of PowerShell scripts
Before you can run a script in the PowerShell console, you have to enable this option. Changing the execution policy requires administrator permissions, that’s why you had to run it as administrator.
If you try to run the script now, it will return an error.
This happens because the execution policy is not defined. To see policies for all scopes, run this command:
The policy is undefined, that’s why it returns an error. Let’s change it.
If you want to return a policy only for the current user, type:
There are 5 types of policies:
|Undefined||If the execution policy is Undefined, it means that it’s Restricted for windows clients and RemoteSigned for Windows Server.|
|Restricted||Permits individual commands, but doesn’t allow to run any scripts.|
|AllSigned||The script runs only if it was signed by a trusted publisher.|
|RemoteSigned||Scripts created on a local machine will run. Scripts created on another machine will run only if they are signed by a trusted publisher.|
|Unrestricted||Any scripts can un, even the unsigned ones. There is a risk of running malicious scripts.|
Set the execution policy to RemoteSigned for the current user:
Or set it for LocalMachine:
Display the policy.
Running scripts downloaded from the Internet
So far we added an execution policy for a script created on the local machine. If you want to run scripts downloaded from the Internet, you have to unblock the file.
- Right-click the file and choose properties.
- On the General tab, Under Security, check Unblock.
- Apply changes.
The next time, you click the General tab, you won’t find this option anymore.
Now, you can run this script the same way as those created on the local machine.
Run PowerShell script
- Click the magnifying glass and type “powershell”.
- Right-click the Windows PowerShell icon and choose Run as Administrator.
- Set the execution policy.
- Navigate to the directory where the file is located.
- Run the script.
Run PowerShell script from the command line
You can also run the PowerShell script from the CMD console.
- Click the magnifying glass and type “cmd”.
- Right-click Command Prompt and click Run as Administrator.
- Type “powershell” into the console. Now, you are using PowerShell.
- Navigate to a directory where the script is located. If you set the execution policy in the previous example, you can run the script.
Run script from another script
With PowerShell, you can create a script the will be executed inside the executed script. Here’s how you can do it.
Modify the first script – script.ps1.
This script will run the message “Hello, World!” and execute the script second_script.ps1.
The second script contains the following command:
If you run the script, you are going to get both messages:
Run PowerShell script from Task Scheduler
If you want to run a script every 5 minutes, 10 seconds, etc., you have to create a scheduled task. This can be achieved using Task Scheduler.
- To open it, write “Task Scheduler” in the search box on the taskbar.
- From Action on the right, choose Create Basic Task.
- Name this task and click Next.
- Now, you can specify when do you want the task to start. Let’s choose Daily.
- Choose the start time and how often the task should be repeated.
- In the next step, choose Start a program.
- As a program script, type “PowerShell” and add the path of the script as an argument.
- On the last window, click Finish.
The task scheduler is often used to run applications at startup. This is one of the options to choose from.
Run PowerShell script with parameters
You can run PowerShell scripts with arguments passed through the command line. Within a script, you can refer to arguments using the $args array.
Indexing starts from 0, so args is the first element, args is the second element, etc.
Let’s create another script called whoami.ps1 with the following code:
Run the following command:
Each argument is separated by a space. You can pass a variable number of arguments. If you want to display all of them you can use a loop.
Create a new strict called loop.ps1 and enter this code:
Now, run this command:
The result of this code is the list of all arguments passed through the command line.
Run PowerShell script from the batch file
Windows allows scripts to be run using batch files.
First, create a batch file and name it “script.bat”.
Insert the following code into a file:
This file sets the execution policy and runs the PowerShell script.
If you had everything set before, you can run the shorter version:
Run PowerShell script double click
There are two ways you can run a script by double-click. The first one is by using the batch file, and the second one is by executing the PowerShell script directly.
If you double-click the batch file it’s going to execute, but it will disappear right after the execution. Sometimes you may want to see the execution results, when you have a result you want to read. It’s true in this case.
You can fix this problem by adding “pause” at the end of the script. It’s going to look like this:
Let’s take a look at how to run PowerShell scripts directly.
The first thing you have to do is to select the default application to execute the ps1 scripts.
To do it, right-click the script and choose Change.
Now, select Windows PowerShell from the list. If it’s not on the list, scroll to the end and choose More Apps, and Look for another app on this PC.
Search for the PowerShell application.
If you double-click the script now, it will disappear the same way as the batch script. You have to add “pause” at the end of the script, the same way as before.
Run the script.