Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


March 2004

Managing Autorun Applications

Take control of your startup programs
RSS
Subscribe to Windows IT Pro | See More Windows NT 4.0 Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Newer versions of Windows (i.e., Windows Server 2003, Windows XP, Windows 2000, and even Windows NT 4.0 in some cases) have an environment variable called pathext that contains a list of extensions for executable files (with the exception of .pif and .lnk). The code at callout B first determines whether the pathext environment variable exists. If the variable doesn't exist, the script creates a temporary variable and uses default values for the .bat, .cmd, .com, and .vbs file extensions. The code then splits the list of extensions into an array of strings (one string per file extension) and reassembles the strings into one string that contains a pipe character (|) between the extensions. A subroutine later in the script uses the resulting string in a regular expression to determine whether a file is an executable file.

The code at callout C obtains the startup items from the registry and the system disk. The code at callout D then either removes startup items or simply displays the list, depending on which parameters the user specifies on the command line when running the script.

The ProcessKey() subroutine enumerates each value in a specified registry subkey and creates an anonymous hash that contains the value (and other associated information), as the code at callout E shows. The hash is then pushed to the @ValueList array. This array of anonymous hashes is returned at the end of the subroutine.

The ProcessDir() subroutine performs the same function for a startup directory that ProcessKey() performs for a registry subkey—it enumerates each file in a specified directory and all its subdirectories. The script passes the paths to the current user and the All Users startup directories to this subroutine. If the subroutine finds a shortcut file in the directory, the code at callout F uses the Win32::Shortcut extension to discover the shortcut's properties. The script then attempts to discover the shortcut's path (also known as the shortcut's target) and extract the target file's description. When the description is available, the script will display it; otherwise; the script will display the shortcut's filename. Finally, the script pushes an anonymous hash to a lexically scoped temporary array called @ValueList and returns the array to the caller. When the ProcessDir() subroutine finds an executable file (according to the file's extension), the subroutine attempts to extract file information using the same method it used for shortcut files, as the code at callout G shows.

When the user specifies a startup item to be removed, the script calls the Remove() subroutine. This subroutine determines whether the specified startup item is in the registry or is a file on a hard disk, as the code at callout H shows. When the entry is in the registry (as a subkey value), the script removes it from the registry. When the entry is a file, the script deletes it from the system disk. These removals and deletions are permanent. Neither deleted startup files nor deleted registry values go into the Recycle Bin.

The GetSpecialDirectory() subroutine returns the path to a specified (i.e., special) directory. For example, the My Documents directory can be on a local drive or can point to a shared directory on a remote server. This subroutine will discover exactly where this directory is. The subroutine in the code at callout I accepts a CSIDL value. CSIDL values identify Windows special folders (e.g., My Documents, Temporary Internet Items). You can find a list of CSIDL values in the Windows software development kit (SDK) shlobj.h header file. The GetSpecialDirectory() subroutine calls the SHGetFolderPath() function that the script loaded earlier. The script's GetSpecialDirectory() function first allocates a buffer—the $pszPath variable—to hold the path. The path that the function returns is in Unicode format. The script attempts to clean up the buffer by removing all NUL characters (\x00), essentially converting the string to ANSI format.

This pseudo conversion from Unicode to ANSI is a wicked hack for several reasons. If your directory path contains Unicode characters, the subroutine will mangle the path by removing all NUL characters, causing the subroutine to treat the string as ANSI and further processing of the path to fail. The script uses this hack when you're using a version of Perl, such as version 5.8 or version 5.6, that can't handle Unicode and UTF-8 character strings. If you need to support Unicode characters, rewrite this subroutine to support Perl's newer Unicode support.

Running the Script
DumpStartups.pl is easy to run. If you run it without specifying switches, the script simply displays a list of all startup items, each with an index number. To remove a particular startup item, you use the /r switch followed by the item's index number. For example, if you want to remove the third and eighth startup items in the list, you'd type

perl DumpStartups.pl /r 3 /r 8

After the script removes an entry, the indexes might change, so you should rerun the script without parameters to display an up-to-date index list.

The Joy of Control
I typically write scripts to solve problems that I encounter every day. I wrote the DumpStartups.pl script out of frustration and a desire to gain back control of my machines. I run this script every time I install a new software application or component.

End of Article

   Previous  1  [2]  Next  


Reader Comments
I do not have perl installed. Is there a VBScript edition of the script.
Thanks

Ralph Elmerick March 03, 2004


A simpler solution:
Download the free autoruns from www.sysinternals.com

Áðüóôïëïò ÖþôáêÝëçò March 11, 2004


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

How can I stop and start services from the command line?

...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events Introduction to Identity Lifecycle Manager "2"

Power Up! With Virtualization Online Conference

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing