1

I recently heard from someone that Windows Admins should use CMD logon scripts over BAT logon scripts, as the run or execute faster. Apparently BAT scripts are notoriously slow.

I've done a bit of a google and I can't find any evidence to backup that claim. I'm just wondering if this is a myth or if anyone can know anymore about this?

3
  • 2
    .... How is that? They're exactly the same thing! Sep 29, 2015 at 3:16
  • Perhaps OP meant .vbs vs .bat?
    – Peter
    Sep 29, 2015 at 4:10
  • 5
    My understanding is there are some slight differences in how BAT and CMD operate, some of which is discussed here.
    – Hammer
    Sep 29, 2015 at 4:25

1 Answer 1

0

Regardless of some differences in how BAT and CMD scripts operate as discussed here‌ (by virtue of meritorious Hammer's comment), commands are parsed and executed one after another hereby remembering next command offset (0 at starting point) and opening the script file again from disk for next command.

1000 commands in a script would imply 1000 disk operations (open-read-close) on the same file. For he sake of exactness, I'm not telling about lines but about commands.
That's real source of both BAT and CMD scripts slowness.

For proof: run a simple sample script ignoring hint to erase the file; the script would type itself:

==> 725431.cmd
725431.cmd script. Please follow instructions.
Press any key to continue . . .
Please erase d:\bat\725431.cmd file prior to continuing.
Press any key to continue . . .
you didn't erase d:\bat\725431.cmd file prior to continuing?
---
@echo off
echo %~nx0 script. Please follow instructions.
pause
echo Please erase %~f0 file prior to continuing.
pause
echo you didn't erase %~f0 file prior to continuing?
echo ---
type "%~f0"

Run above script observing hint to erase the file; The batch file cannot be found error shows that batch-parser cannot get next echo command:

==> 725431.cmd
725431.cmd script. Please follow instructions.
Press any key to continue . . .
Please erase d:\bat\725431.cmd file prior to continuing.
Press any key to continue . . .
The batch file cannot be found.

For the sake of completeness, here is a standard file system error message:

==> type 725431.cmd
The system cannot find the file specified.

On the contrary, similar (for instance) PowerShell script is cached in memory. Again, run sample script ignoring hint to erase the file first; the script would type itself:

PS D:\PShell> D:\PShell\SF\725431.ps1
D:\PShell\SF\725431.ps1 script. Please follow instructions.
Press Enter to continue...: 
Please erase D:\PShell\SF\725431.ps1 file prior to continuing.
Press Enter to continue...: 
you didn't erase D:\PShell\SF\725431.ps1 file prior to continuing?
---
echo "$PSCommandPath script. Please follow instructions."
pause
echo "Please erase $PSCommandPath file prior to continuing."
pause
echo "you didn't erase $PSCommandPath file prior to continuing?"
echo "---"
Get-Content $PSCommandPath

Run the script observing hint to erase the file. Doing that would show that the latter echo and Get-Content were cached in memory:

PS D:\PShell> D:\PShell\SF\725431.ps1
D:\PShell\SF\725431.ps1 script. Please follow instructions.
Press Enter to continue...: 
Please erase D:\PShell\SF\725431.ps1 file prior to continuing.
Press Enter to continue...: 
you didn't erase D:\PShell\SF\725431.ps1 file prior to continuing?
---
Get-Content : Cannot find path 'D:\PShell\SF\725431.ps1' because it does not ex
ist.
At D:\PShell\SF\725431.ps1:8 char:1
+ Get-Content $PSCommandPath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (D:\PShell\SF\725431.ps1:String) 
    [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo 
   ntentCommand

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .