How Can I Get a List of All My Internet Cookies?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I get a list of all my Internet cookies?

— ZO

SpacerHey, Scripting Guy! AnswerScript Center

Hey, ZO. OK, we have to admit it: you tricked the Scripting Guys, and we fell for it, hook, line, and sinker. As soon as we saw your email – and the word “cookies” – there was a mad scramble to see who would get to answer this question. It was only after the dust settled and there was only one Scripting Guy left standing that we realized you were talking about Internet cookies rather than, say, chocolate chip cookies. Oh, well.

Note. Actually, that’s not true: usually when a new question is received there’s a cloud of dust alright, but it’s dust kicked up by all the other Scripting Guys running away as fast as they can so they won’t have to answer the thing. As usual, that leaves poor Jean – the Cinderella of the scripting world – to do all the work all by herself. But will you ever hear a single word of complaint from Jean? No, never. (Editor’s Note: Honest, all I changed in this paragraph were a couple of typos. Someone tried to spell Jean as G-r-e-g.)

Oh, sure, you might hear several thousand words of complaint. But never just a single word. (Editor’s Note: OK, maybe G-r-e-g was right.)

Admittedly, tallying up your Internet cookies isn’t half as much fun as tallying up your chocolate chip or oatmeal raisin cookies. On the other hand, it is easier to write a script that returns a list of all your Internet cookies:

Const HKEY_CURRENT_USER = &H80000001

strComputer = “.” Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set objRegistry = GetObject(“winmgmts:\\” & strComputer & “\root\default:StdRegProv”)

strKeyPath = “Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders” strValueName = “Cookies” objRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strFolder

Set colFileList = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_Directory.Name='” & strFolder & “‘} Where ” _ & “ResultClass = CIM_DataFile”)

For Each objFile In colFileList Wscript.Echo objFile.FileName Next

Before we get started we should point out that if all you want to do is retrieve the list of cookies found on your computer (or, more correctly, found in your user profile) then this script is perhaps a tad bit more complicated than it needs to be. That’s because we wanted a script that could retrieve information from a remote computer as well as from a local computer. If all you care about is the local machine, however, then this script works equally well:

Const COOKIES = &H21&

Set objShell = CreateObject(“Shell.Application”) Set objFolder = objShell.Namespace(COOKIES) Set objFolderItem = objFolder.Self Wscript.Echo objFolderItem.Path

Set colItems = objFolder.Items For Each objItem in colItems Wscript.Echo objItem.Name Next

So what’s the difference between the two scripts? Well, the second script relies on the Shell.Application object to locate the Cookies folder. That’s easy for the Shell object to do because the Cookies folder happens to be a “special folder,” a folder whose location is well-known – and tracked by – the operating system. As you can see, we simply call the Namespace method (passing along the hex value that represents the Cookies folder) and then let the Shell object take it from there.

Unfortunately, though, the Shell object works against only the local computer. If you want to return cookie information from a remote machine you need to use WMI to determine the location of the Cookies folder on that computer, then let WMI retrieve the information for you. And that’s exactly what our first script does.

To do all of that the script starts out by defining a constant named HKEY_CURRENT_USER; we’ll use this later on to indicate the registry hive we want to work with. We then make two different connections to the WMI service. The first connection binds us to the WMI service itself:

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Meanwhile, the second connection binds us to the System Registry provider:

Set objRegistry = GetObject(“winmgmts:\\” & strComputer & “\root\default:StdRegProv”)

Could we have done this by using (and then reusing) a single connection? Sure. We just thought it would be easier for people to follow the logic if we used one object reference named objWMIService to represent the WMI service and a second object reference (objRegistry) to represent our connection to the registry provider. But if you prefer to carry out this task using just one object reference, well, so much the better.

Our next step is to read the Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Cookies registry value in order to determine the location of the Cookies folder. To do that we start off by defining two variables: strKeyPath, which represents the registry path within the HKEY_CURRENT_USER hive; and strValueName, which represents the registry value we want to read (Cookies):

strKeyPath = “Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders”
strValueName = “Cookies”

With our two variables defined we can then call the GetStringValue method to retrieve the folder location:

objRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strFolder

As you can see, we pass GetStringValue four parameters: our HKEY_CURRENT_USER constant; strKeyPath; strValueName; and strFolder, an “out” parameter that the method will fill with the path to the Cookie folder (for example, C:\Documents and Settings\kenmyer\Cookies).

And once we have the folder location safely stashed away in the variable strFolder we can then use this crazy-looking query to return a collection of all the files found in that folder:

Set colFileList = objWMIService.ExecQuery _
    (“ASSOCIATORS OF {Win32_Directory.Name='” & strFolder & “‘} Where ” _
        & “ResultClass = CIM_DataFile”)

The rest – as you no doubt know – is easy: we simply set up a For Each loop to walk through all the files (cookies) in the collection, echoing back the name of each one:

For Each objFile In colFileList
    Wscript.Echo objFile.FileName

Piece of cake. Um, so to speak.

Incidentally, you will also see cookie files in the Temporary Internet Files folder as well as in the Cookies folder. In general, the cookies found in the Temporary Internet Files folder are pointers to the real cookies; that’s why emptying the Temporary Internet Files folder will often leave the cookies in place. If all you want to do is list the cookies then you can just focus on the Cookies folder and ignore anything in the Temporary Internet Files folder; most (if not all) of the cookies in the Temporary Internet Files folder will simply be duplicate pointer entries.

Now, as for those other cookies, well, feel free to send them to the following:

The Microsoft Scripting Guys
Microsoft Corporation
Building 43/2023
One Microsoft Way
Redmond, WA 98052-6399

We promise to, uh .. take care of … any chocolate chip, oatmeal raisin, or other non-Internet cookies we receive.


Comments are closed. Login to edit/delete your existing comments