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 subkeyit 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 bufferthe $pszPath variableto 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
Thanks
Ralph Elmerick March 03, 2004