Run Batch Files Windows 10

Run Batch Files Windows 10 9,5/10 9104 reviews
Technical Level : Intermediate

How to Create a Batch File in Windows 10? Open a Notepad, type there ipconfig /flushdns, and save it as anyname.bat. Now, every time you need to flush the DNS, just double click that.bat file, and the command will execute. No more typing all the time. The code should look like this. Batch Files As Administrator. To auto-run a batch file as administrator, you need to first create the batch file, and then create a shortcut for it. To create a shortcut, right-click the file, and select Send to.


Summary

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. The method shown can be used for any batch file, vbs file or other script file.

This Wiki article assumes sufficient general skills with batch files & Task scheduler so that it can focus on achieving a minimised window & a specific icon.

This Wiki article is for Windows 10 users. The methods for Windows 7 & Windows 10 differ. Windows 7 users should refer to the companion article - Make Windows 7 Task scheduler run a batch file minimised & with a specific icon.

Denis


Details

1Security & file locations

1.1All 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.Pdf417 barcode generator driver license.

1.2My 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 onmyself.

1.3The procedure for setting up C:Tools is in the Annex.

1.4I 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:ToolsBackup, C:ToolsPhoneSync, C:ToolsPVR.

1.5I also have a corresponding group of subfolders within

C:UsersMickyMorDocumentsToolsDevn

where I do all the drafting & amendments.Copying updated versions to C:Tools becomes, in effect, the act of publication.

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

2.1Write it in a convenient drafting folder such as

C:UsersMickyMorDocumentsToolsDevnThisBatch

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

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

3Create a batch file shortcut - ThisBatch.lnk

3.1In C:ToolsThisBatch, 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.2Tidy 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.3Right-click on ThisBatch.lnk & select Properties.

3.4Click 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.5Click 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.6A 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.7Select from any of the available icons, click on OK then click on the Apply button to save the change.

3.8If 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%system32SHELL32.dll {this is the default shown when you first open the Change icon dialog}

-%SystemRoot%system32imageres.dll

-%systemroot%system32moricons.dll

-C:Windowsexplorer.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, Developpers Iconset (105 icons)Sekkyumucontains several freeware icons for download.

3.10It 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.11If 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.12Test ThisBatch.lnk now so that its behaviour can be relied upon when testing the whole job.

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

4Write an initiating vbs file - StartThisBatch.vbs

4.1Write a vbs file that runs ThisBatch.lnk rather than ThisBatch.bat itself.

Set WshShell = CreateObject('WScript.Shell')

WshShell.Run chr(34) & 'C:ToolsThisBatchThisBatch.lnk' & Chr(34), 7

Set WshShell = Nothing

4.2The trailing argument 7 runs ThisBatch.lnk minimised, overriding the shortcut’s own properties [see para 3.4 above].

4.3I cannot remember why I added the last line Set WshShell = Nothing [it was added quite some time ago].I think I found some advice that it was a useful precaution to take in case other vbs procedures were to be used later on in the same Windows session but I am not sure at all.

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

4.5Copy StartBatch.vbs to C:ToolsThisBatch. Give Admin permission when challenged.

5Set up a TS Task to run StartBatch.vbs

5.1Create a Task in Task Scheduler.

Windows 10 Batch File Commands

5.2Set up its Properties, Actions tab entry:-

Action: Start a program

Program/script: c:windowssystem32wscript.exe

Argument: 'C:ToolsThisBatchStartThisBatch.vbs'

5.3Set whatever other Properties are desired, such as its schedule.

6Test the whole job

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

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

6.3A 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 and it can be edited in Notepad].

Acknowledgements:Ramesh Srinivasan

AnnexUse a protected folder for batch files, vbs files & other script files

A1Why bother

A1.1I 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.2Malware 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.3If a user or an application, for example, tries to copy anything into C:Program FilesVideoLANVLC 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:UsersMickyMorDocuments, 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.4The 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.5However, 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:UsersMickyMorDocumentsMyOwnBatchFiles 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.6In 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.7An 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.8This 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.

A2Set up the protected folder

A2.1In any user account, open File explorer then create folder C:Tools.

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

A2.3Click 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.4In 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.5In 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.6In 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.7Back in the Advanced security settings for Tools dialog, click on the Apply button in the bottom right corner.

A2.8At 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.9Set 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.10This is what the Advanced security settings for Tools dialog will look like when you have finished:-

A3Test the new folder permissions

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

A3.2Open 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.3Try 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.

A4Use the protected folder

A4.1Copy 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.2Update shortcuts & Task scheduler entries to refer to the new locations.

Can T Run Batch Files Windows 10

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