To run a PowerShell script using the Task Scheduler, PowerShell's Execution Policy typically needs to be set to RemoteSigned. Use the Get-ExecutionPolicy cmdlet to view the current ExecutionPolicy.
If the Execution Policy is not set to RemoteSigned, use the the Set-ExecutionPolicy cmdlet to set the Execution Policy to RemoteSigned.
Lets create a PowerShell script that appends the current date and time to a file named DateTime.txt.
- Select the Windows Start icon, in search type PowerShell, and select PowerShell ISE
- In the top panel of PowerShell ISE, enter the following code
$date = (Get-Date).ToString('MM/dd/yyyy hh:mm:ss') $date >> C:\path\to\DateTime.txt
- Select File > Save as, and save the file as C:\Users\<username>\PowerShell_Scripts\current_date_time.ps1
- Press the green play button in PowerShell ISE
- Open the C:\path\to\DateTime.txt file to ensure the text file contains the current date/time.
- Press the green play button again, and ensure the DateTime.txt file contains a new line with the latest date/time.
To schedule the script using Task Scheduler:
- Select the Windows Start icon, type task in search, and select Task Scheduler
- On the right side of the Task Scheduler, select Create Task
- In the Create Task dialog box, make the following entries:
- General Tab
- Give this task a name, such as PowerShell Currnet Date Time
- At When running the task, use the following user account, ensure the user account being used has permission to write to all of the directories in your script.
- Bullet Run whether user is logged on or not and check Do not store password
- Triggers Tab
- Select New
- Schedule the task to run today, just a few minutes in the future.
- Actions Tab
- Select New
- Keep the action as Start a program
- In Program/script type C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- In the Add Argument box, type C:\Users\<username>\PowerShell_Scripts\current_date_time.ps1
- Leave the Start In box empty
- Select OK
- Conditions Tab
- Remove the check from Start the task only if the computer is on AC power
- Settings Tab
- Check Run task as soon as possible after a scheduled start is missed
- In Stop the task if it runs longer than, set the drop-down to 1 hour
- Select OK
- Select OK to save the Task
Verify the task was successfully run:
In the left panel of Task Scheduler, select Task Scheduler Library, and the newly created PowerShell Current Date Time Task will be listed in the list of tasks. Before the task is run for the first time, Last Run Time will be never. After the task schedule has passed, right-click on Task Scheduler Library and select Refresh. The Last Run Time should display the time the task was run. Navigate to C:\Users\<username>\PowerShell_Scripts\ and open the current_date_time.txt file. Ensure the text file contains the date/time the task was scheduled to run.
Powershell and Task Scheduler nuiances
Some PowerShell scripts may work properly when in PowerShell, but fail to work properly when using Task Scheduler. Following are common issues:
- Mapped network drive UNC - If your PowerShell script includes remote storage, use the UNC path to the remote storage in the PowerShell script. For example, use \\server\share instead of X:\. PowerShell will not use the network drives that are mapped in File Explorer.
- Mapped network drive username / password - If your PowerShell script includes remote storage, and a username and password is required to map the network drive, the script may require logic to connect to the share with a username and password. When running the script in PowerShell, an error may appear, such as "Multiple connections to a server or shared resource by the same user." This message can be ignored.
$FS2 = "\\fs.example.com\share" $username = "Your Username" $password = "Your Password" net use $FS2 $password /USER:$username
One issue with the scheduled task is that the PowerShell GUI appears every time the task is run. This is particularly problematic for scripts that are scheduled to run frequently, such as once every minute. To run the script silently, click here to download and install Powershell Community extensions (or go to http://pscx.codeplex.com/).. Then update the scheduled task, adding -WindowStyle Hidden to the Add Arguments field (example: C:\Users\<username>\PowerShell_Scripts\current_date_time.ps1 -WindowStyle Hidden).