Make Task scheduler run a batch file minimised & with a specific icon


  1. Posts : 16,958
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #1

    Make Task scheduler run a batch file minimised & with a specific icon


    Introduction

    I have a batch file that I run minimised & with the icon I have chosen. I am happy with the way it runs as I do not need to see the Command window every time and having my chosen icon on the TaskBar shows me at a glance what is running.

    I also want to run this batch file from Task scheduler in the same way - minimised & with my chosen icon. Windows Task scheduler does not want to cooperate; it opens a command window and the TaskBar icon that appears is the default Cmd icon.

    However, Windows Task scheduler can be persuaded to cooperate by creating intermediate files that initiate the desired batch file.

    This method can be used for any batch file, vbs file or other script file.


    This article assumes sufficient general skills with scripting & Task scheduler so that it can focus on achieving the minimised window & specific icon.


    1 Security & file locations


    1.1 All my batch files, vbs files & other script files are saved in subfolders of C:\Tools, a folder that has particular permissions set up to protect its contents against undesirable access such as malware infiltration. Setting up this protected folder can be done very quickly and easily and it does not affect running the files.

    1.2 My C:\Tools folder forces Admin prompts for all changes [additions, amendments & deletions]. I do not amend my batch files, vbs files & other script files very often so I do not mind the inconvenience of forcing Admin prompts on myself.

    1.3 The procedure for setting up C:\Tools is in the Annex to this article.

    1.4 I have subfolders for each task or group of tasks so I don’t get confused about which file is part of which procedure; I have, for example, C:\Tools\Backup, C:\Tools\Network, C:\Tools\PhoneSync, C:\Tools\PVR.

    1.5 I also have a corresponding group of subfolders within C:\Users\MickyMor\Documents\ToolsDevn where I do all the drafting & amendments. Copying updated versions to C:\Tools becomes, in effect, the act of publication.


    2 Write the batch file that will do the required job - ThisBatch.bat

    2.1 Write it in a convenient drafting folder such as C:\Users\MickyMor\Documents\ToolsDevn\ThisBatch

    2.2 Test ThisBatch.bat on its own now so that its function can be relied upon when testing the whole job later on.

    2.3 Create the folder C:\Tools\ThisBatch and copy ThisBatch.bat into it. Give Admin permission when challenged.


    3 Create a batch file shortcut - ThisBatch.lnk

    3.1 In C:\Tools\ThisBatch, right-click on ThisBatch.bat and select Create shortcut. Accept the offer to create the shortcut on your desktop then move it into your drafting folder.

    3.2 Tidy up the name so it appears as ThisBatch. It is actually called, in full, ThisBatch.lnk but the file extension is deliberately hidden by the system.

    3.3 Right-click on ThisBatch.lnk & select Properties.

    3.4 Click on the Run entry and select Minimised then click on the Apply button to save the change. This particular setting prepares the shortcut for independent use but is not used here - it is overridden by a parameter in section 4.

    3.5 Click on the Change icon… button. You will see a warning that ThisBatch.bat contains no icons. Accept the warning & the Change icon dialog then appears.

    3.6 A gallery of icons appears from which you can select one that is in some way indicative of the job that ThisBatch.bat will be doing.

    3.7 Select from any of the available icons, click on OK then click on the Apply button to save the change.

    3.8 If you cannot find an appropriate icon then you can open other icon files to look there. In the Change icon dialog’s Look for icons in this file: field, type in the path to other icon sources then press your keyboard’s
    Enter/Return key or click on the Browse button to reach them. Icon sources include
    - %SystemRoot%\system32\SHELL32.dll {this is the default shown when you first open the Change icon dialog}
    - %SystemRoot%\system32\imageres.dll
    - %systemroot%\system32\moricons.dll
    - C:\Windows\explorer.exe and other exe files in the Windows folder
    - Application exe files in C:\Program Files and C:\Program Files (x86)
    - Individual .ico files that applications have created
    - Individual .ico files that you have created
    - Individual .ico files that you have downloaded

    3.9 Always scan any downloaded icon with your antimalware application. Then right-click on the icon, select Properties and look near the bottom of the dialog for an entry Security: This file came from another computer and might be blocked to help protect this computer. Now that you have scanned the icon with your antimalware application, you can click in the checkbox to Unblock it. Merely as an example, Developers Icons by Sekkyumu contains several freeware icons for download.

    3.10 It can be worth taking some time to select an icon because, in operation, the icon will appear in the TaskBar as your only indicator of what is running.

    3.11 If your batch file requires arguments when initiated from Task scheduler then I have found putting them into the shortcut is the simplest approach. Add the arguments to the end of the shortcut’s Target field.

    3.12 Test ThisBatch.lnk now so that its behaviour can be relied upon when testing the whole job later on.

    3.13 Copy ThisBatch.lnk into C:\Tools\ThisBatch. Give Admin permission when challenged.


    4 Write an initiating vbs file - StartThisBatch.vbs

    4.1 Write a vbs file that runs ThisBatch.lnk rather than ThisBatch.bat itself.
    Code:
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run chr(34) & "C:\Tools\ThisBatch\ThisBatch.lnk" & Chr(34), 7
    Set WshShell = Nothing

    4.2 The trailing argument 7 runs ThisBatch.lnk minimised, overriding the shortcut’s own properties [see para 3.4 above]. If you wanted it to run hidden you would use 0 instead of 7 but nothing would then be shown on the Taskbar when it ran.

    4.3 The last line Set WshShell = Nothing is merely a useful precaution to take if other vbs procedures are to be used later on in the same Windows session.

    4.4 Test StartThisBatch.vbs now so that its behaviour can be relied upon when testing the whole job later on.

    4.5 Copy StartBatch.vbs to C:\Tools\ThisBatch. Give Admin permission when challenged.


    5 Set up a TS Task to run StartBatch.vbs

    5.1 Create a Task in Task Scheduler.

    5.2 Set up its Properties, Actions tab entry:-
    Action: Start a program
    Program/script: c:\windows\system32\wscript.exe
    Argument: "C:\Tools\ThisBatch\StartThisBatch.vbs"

    5.3 Set whatever other Properties are desired such as its schedule and, if appropriate, its General tab - Run with highest privileges checkbox.


    6 Test the whole job

    6.1 Right-click on the task in Task scheduler and select Run.

    6.2 ThisBatch.bat’s chosen icon will appear minimised in the TaskBar while it runs. Clicking on it will open its window.

    6.3 A backup of all files can now usefully be made. The TS task itself can be backed up by exporting it [it will be in xml format which can be edited in Notepad].


    Denis

    Acknowledgements: Ramesh Srinivasan [Winhelponline.com]




    Annex Use a protected folder for batch files, vbs files & other script files

    A1 Why bother

    A1.1 I have put the discussions, explanations, ifs, buts & maybes in this section so that the procedure in A2 can be straightforward. You do not have to read this section. You can skip straight to A2.

    A1.2 Malware tries to infiltrate computers. Windows’ protection measures include limiting access permissions for Windows folders & for the folders in which applications are installed such as C:\Program files.

    A1.3 If a user or an application, for example, tries to copy anything into C:\Program Files\VideoLAN\VLC or to change its existing content then an Admin prompt forces elevated permission to be granted [strictly speaking, I should use the terms Elevation UI, Consent UI, Credential UI or Secure Desktop mode instead of the commonly-used term Admin prompt]. Unlike granting access permission to folders within C:\Users\MickyMor\Documents, which has a permanent effect, this elevated permission only applies to the current task. If another task later requires access to anything within C:\Program Files then a new Admin prompt forces Admin permission to be sought once again.

    A1.4 The requirement for Admin approval every time achieves a significant level of protection against malware, particularly if the user account control [UAC] setting is left in its default state or a higher level so that Admin prompts are generated for software installations & other overall system changes against the backdrop of the dimmed screen [This condition is when the Admin prompt is referred to as being in Secure Desktop mode]. Microsoft believe that, with UAC set this way, malware cannot get onto a system then run without a user’s express consent.

    A1.5 However, malware can attempt to exploit resources that are common to both the Standard user’s environment & the environment of an operation for which Admin permission has been granted. For example, an executable that is held in a folder such as C:\Users\MickyMor\Documents\MyOwnBatchFiles is not given the system protection afforded to executables within C:\Program files or C:\Program files (x86). Therefore, even if an Admin prompt is correctly generated and correctly identifies the executable concerned, that executable itself might have been hijacked by malware unless the executable is signed & that signature is checked.

    A1.6 In other words, malware can attempt to infiltrate our own batch files, vbs files & other script files as a stepping stone to infiltrating the entire computer because we do not apply recognised signatures to these files.

    A1.7 An effective solution is to extend the system protection afforded to executables within C:\Program files or C:\Program files (x86) to the folders within which batch files, vbs files & other script files are stored.

    A1.8 This can be done very quickly and easily and it does not affect running the files. The user can create a protected folder and put all batch files, vbs files & other script files within it. This is what I do and I recommend that everybody does the same.


    A2 Set up the protected folder

    A2.1 In any user account, open File explorer then create folderC:\Tools.

    A2.2 Right-click on this new folder, select Properties,Security, Advanced to open the Advanced security settings for Tools dialog.

    A2.3 Click on the Disable inheritance button near the bottom of the dialog. In the confirmation dialog that appears, click on Convert inherited permissions into explicit permissions on this object.

    A2.4 In the list of user accounts & user account groups, select Administrators [Administrators in the plural not Administrator in the singular] then click on Edit. Permissions for Administrators will probably need no adjustment so this is just a check. Set the checkbox for Full control [which will, in turn, set all the others except Special permissions]. Click on OK.

    A2.5 In the list of user accounts & user account groups, select Authenticated users if it exists then click on Edit. Clear the checkboxes for Full control, Modify, Write. Set the checkbox for Read & execute [which will, in turn, set those for List folder contents, Read]. Click on OK.

    A2.6 In the list of user accounts & user account groups, select Users then click on Edit. Permissions for Users will probably need no adjustment so this is just a check. Clear the checkboxes for Full control, Modify, Write. Set the checkbox for Read & execute [which will, in turn, set those for List folder contents, Read]. Click on OK.

    A2.7 Back in the Advanced security settings for Tools dialog, click on the Apply button in the bottom right corner.

    A2.8 At the top of the dialog, the owner is shown as the current user, click on Change then provide Admin permission if prompted. In the text entry box, enter the word Administrators [Administrators in the plural not Administrator in the singular; do not accidentally enter a space after the word because there is no such user group], click on Check names, OK.

    A2.9 Set the checkbox for Replace owner on subcontainers and objects then click on OK. Then click on OK in the folder’s Properties dialog in order to return to File explorer.

    A2.10 This is what the Advanced security settings for Tools dialog will look like when you have finished:-





    A3 Test the new folder permissions

    A3.1 Open C:\Tools then right-click over a blank area and hover over New. Only a new folder is permitted not a new file.

    A3.2 Open C:\Tools then attempt to create a subfolder within it. You will be challenged for Admin permission and the subfolder will not be created until you give it.

    A3.3 Try to copy an existing folder or file into C:\Tools or its subfolder. You will be challenged for Admin permission and the folder or file will not be pasted in until you give it.


    A4 Use the protected folder

    A4.1 Copy all your existing batch files, vbs files & other script files into subfolders of C:\Tools. Copying is better than moving because moving can create chaotic amalgamations of security permissions.

    A4.2 Update shortcuts & Task scheduler entries to refer to the new locations.

    A4.3 Consider creating a development folder ToolsDevn somewhere convenient so that you have a home for drafting new versions & for saving archived versions.
      My Computer


  2. Posts : 7,607
    Windows 10 Home 20H2
       #2

    Try3 said:
    having my chosen icon on the TaskBar
    I like having my chosen icon in the notification area. Is that possible?
      My Computer


  3. Posts : 16,958
    Windows 10 Home x64 Version 22H2 Build 19045.4170
    Thread Starter
       #3

    No, it is not.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:49.
Find Us




Windows 10 Forums