Both CMD and PowerShell are included by default in Windows and are responsible for working through commands and scripts that will allow us to have greater control over the operating system. However, in most cases, to be able to use and take advantage of its advantages it will be necessary to have advanced knowledge of Windows.
And is that using the PowerShell option we can schedule, modify and delete tasks. This can be very useful when we need to speed up the process of creating tasks on several devices or if we need to create a script that needs to interact with the console. That is why today we are going to see how we can create or delete tasks and programs, using Windows PowerShell.
What is PowerShell and what is it for
Also known as Windows PowerShell is a command line utility It is presented as an improved and advanced version of the traditional Command Prompt, which will allow us to execute different command unions, called Scripts, thus facilitating the creation, configuration and automation of multiple tasks. It is based on .NET CLR so it is ideal for process automation. Therefore, we are facing a tool that, given its flexibility, is usually used by both engineers and system analysts on a regular basis.
Unlike other command line interfaces like CMD, PowerShell can work with objects beyond a simple command. Thanks to its complete scripting interface, we can carry out specific functions under the Windows operating system, such as, for example, launching different commands to automate different tasks. In addition, it offers us the possibility of being able to add the console other commands that have been created by the users themselves.
Although for many users, especially with basic knowledge about the Microsoft operating system, PowerShell is a completely unknown tool, the truth is that it was already compatible with Windows XP, with the exception that it was necessary to download it to be able to use it. Since the arrival of Windows 10, PowerShell is already pre-installed, taking on greater prominence and visibility so that access is easier within the Microsoft operating system.
Create a scheduled task with PowerShell
The first thing we must do is open PowerShell by granting it administrator privileges. To do this, we will use the Start menu search engine where we will write powershell. Once the application appears, we will click on Run as administrator.
Next, a window will appear warning us that the application will make changes to the device, so we click Yes and the PowerShell screen will appear visibly, from where we will begin to write the necessary commands to create our scheduled tasks.
Once the PowerShell screen has appeared, we must enter the following command:
$action = New-ScheduledTaskAction -Execute 'app-path'
Subsequently, we must bear in mind that we must replace the application path with the original path of the application that we want to run. For example, if we want to open Mozilla Firefox we must use the Firefox.exe file, so the full path would be as follows:
$action = New-ScheduledTaskAction -Execute 'C: / Archivos de programa / Mozilla Firefox / firefox.exe'
In the event that we need to create a PowerShell script, it will be necessary to use “-argument” and specify the script. In this way the final result should be such that
New-ScheduledTaskAction -Execute 'powershell.exe' -argument script-path
The next step will consist of setting the time we want to execute the task we want to schedule, being able to choose any time we need. We can choose between the 12 or 24 hour format. In the case that we choose a 12-hour format, we must accompany it with AM / PM at the time specified in the command. For example, we can schedule the program to run once at 07:00 PM or at 19:00 on the same day. It will also be possible to configure it to activate daily, weekly, monthly, etc.
For this we must use the following command:
$trigger = New-ScheduledTaskTrigger -Once -At 07:00PM
Next, we must register the task that we are going to program we must enter the following command, changing DemoName, DemoTask and DemoDescription according to our preferences:
Register-ScheduledTask -Action $action -Trigger $trigger -TaskPath "DemoName" -TaskName "DemoTask" -Description "DemoDescription"
Once this is done, the system will have automated the task that we have scheduled and it will always be executed at the time we have set. As soon as we no longer want to carry out the programming of this task, we must proceed to eliminate it.
Delete a scheduled task with PowerShell
To eliminate any program task, we must start PowerShell again with administrator privileges. We will write powershell in the search engine of the Start menu. Once the application appears, we will click on “Run as administrator”. Next, a window will appear warning us that the application will make changes to the device, so, again, we click on Yes and the PowerShell screen will appear visibly.
Once the PowerShell window has appeared, we must enter the following command, replacing DemoTask with the name of the original task:
Unregister-ScheduledTask -TaskName "DemoTask" -Confirm: $false
Once this is done, we press enter and the task will be eliminated. Optionally, we can write the following command to confirm that the task has been eliminated and make sure that it will no longer run on a scheduled basis. For this we will write:
Get-ScheduledTask -TaskName "DemoTask"
This command will be in charge of confirming that the DemoTask task is no longer available. Once we have completed the steps, we should receive an error message. This will tell us that there is no task with that specific name. This means that the task that we had scheduled has already been correctly eliminated previously.
Manage scheduled tasks with PowerShell
The Windows PowerShell command will also allow us to manage tasks. For this we use the Get-ScheduledTask command that will be in charge of returning all or some of the tasks scheduled by our team.
To carry out this action it will only be necessary to start PowerShell as administrator and once in the window write the following command:
By using only the basic command, all the tasks on the computer will be listed ordered by path. We can see how the path of the task is included, the name of the task and the state it is in. By means of the name we can identify the task while the state section (State) will indicate if it is activated or deactivated.
In PowerShell we can also disable or enable the selected tasks, by using the Disable-ScheduledTask and Enable-ScheduledTask commands. In the case that we execute the commands directly, it will be necessary to specify the name of the task or the routes.
For example, if we want to disable tasks related to Google, we will use the Disable-ScheduledTask command, which will be in charge of disabling it. For this we write the following command.
Get-ScheduledTask -taskname google* | Disable-ScheduledTask
In the event that we want to activate all Google tasks, we will use the command we will use the same command line above except that in this case we will use Enable-ScheduledTask at the end, which will be in charge of ordering the enablement of the task.
Get-ScheduledTask -taskname google* | Enable-ScheduledTask