{"id":67453,"date":"2006-04-26T09:29:00","date_gmt":"2006-04-26T09:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/26\/how-can-i-get-a-list-of-all-my-internet-cookies\/"},"modified":"2006-04-26T09:29:00","modified_gmt":"2006-04-26T09:29:00","slug":"how-can-i-get-a-list-of-all-my-internet-cookies","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-all-my-internet-cookies\/","title":{"rendered":"How Can I Get a List of All My Internet Cookies?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I get a list of all my Internet cookies?<BR><BR>&#8212; ZO<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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 &#8211; and the word \u201ccookies\u201d &#8211; 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 <I>Internet<\/I> cookies rather than, say, chocolate chip cookies. Oh, well.<\/P>\n<TABLE id=\"E3C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. Actually, that\u2019s not true: usually when a new question is received there\u2019s a cloud of dust alright, but it\u2019s dust kicked up by all the other Scripting Guys running away as fast as they can so they won\u2019t have to answer the thing. As usual, that leaves poor <I>Jean<\/I> &#8211; the Cinderella of the scripting world &#8211; to do all the work all by <I>her<\/I>self. But will you ever hear a single word of complaint from <I>Jean<\/I>? No, never. <I>(Editor\u2019s Note: Honest, all I changed in this paragraph were a couple of typos. Someone tried to spell Jean as G-r-e-g.)<\/I><\/P>\n<P>Oh, sure, you might hear several thousand words of complaint. But never just a single word. <I>(Editor\u2019s Note: OK, maybe G-r-e-g was right.)<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Admittedly, tallying up your Internet cookies isn\u2019t half as much fun as tallying up your chocolate chip or oatmeal raisin cookies. On the other hand, it <I>is<\/I> easier to write a script that returns a list of all your Internet cookies:<\/P><PRE class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set objRegistry = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders&#8221;\nstrValueName = &#8220;Cookies&#8221;\nobjRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strFolder<\/p>\n<p>Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name='&#8221; &amp; strFolder &amp; &#8220;&#8216;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In colFileList\n    Wscript.Echo objFile.FileName\nNext\n<\/PRE>\n<P>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\u2019s 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:<\/P><PRE class=\"codeSample\">Const COOKIES = &amp;H21&amp;<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(COOKIES)\nSet objFolderItem = objFolder.Self\nWscript.Echo objFolderItem.Path<\/p>\n<p>Set colItems = objFolder.Items\nFor Each objItem in colItems\n    Wscript.Echo objItem.Name\nNext\n<\/PRE>\n<P>So what\u2019s the difference between the two scripts? Well, the second script relies on the <B>Shell.Application<\/B> object to locate the Cookies folder. That\u2019s easy for the Shell object to do because the Cookies folder happens to be a \u201cspecial folder,\u201d a folder whose location is well-known &#8211; and tracked by &#8211; the operating system. As you can see, we simply call the <B>Namespace<\/B> method (passing along the hex value that represents the Cookies folder) and then let the Shell object take it from there.<\/P>\n<P>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\u2019s exactly what our first script does.<\/P>\n<P>To do all of that the script starts out by defining a constant named HKEY_CURRENT_USER; we\u2019ll 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:<\/P><PRE class=\"codeSample\">Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n<\/PRE>\n<P>Meanwhile, the second connection binds us to the System Registry provider:<\/P><PRE class=\"codeSample\">Set objRegistry = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)\n<\/PRE>\n<P>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.<\/P>\n<P>Our next step is to read the <B>Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\Cookies<\/B> 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):<\/P><PRE class=\"codeSample\">strKeyPath = &#8220;Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders&#8221;\nstrValueName = &#8220;Cookies&#8221;\n<\/PRE>\n<P>With our two variables defined we can then call the <B>GetStringValue<\/B> method to retrieve the folder location:<\/P><PRE class=\"codeSample\">objRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strFolder\n<\/PRE>\n<P>As you can see, we pass GetStringValue four parameters: our HKEY_CURRENT_USER constant; strKeyPath; strValueName; and strFolder, an \u201cout\u201d parameter that the method will fill with the path to the Cookie folder (for example, C:\\Documents and Settings\\kenmyer\\Cookies). <\/P>\n<P>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:<\/P><PRE class=\"codeSample\">Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name='&#8221; &amp; strFolder &amp; &#8220;&#8216;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>The rest &#8211; as you no doubt know &#8211; 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:<\/P><PRE class=\"codeSample\">For Each objFile In colFileList\n    Wscript.Echo objFile.FileName\nNext\n<\/PRE>\n<P>Piece of cake. Um, so to speak.<\/P>\n<P>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 <I>real<\/I> cookies; that\u2019s 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.<\/P>\n<P>Now, as for those <I>other<\/I> cookies, well, feel free to send them to the following:<\/P>\n<P>The Microsoft Scripting Guys<BR>Microsoft Corporation<BR>Building 43\/2023<BR>One Microsoft Way<BR>Redmond, WA 98052-6399<\/P>\n<P>We promise to, uh .. take care of \u2026 any chocolate chip, oatmeal raisin, or other non-Internet cookies we receive.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get a list of all my Internet cookies?&#8212; ZO 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 &#8211; and the word \u201ccookies\u201d &#8211; there was a mad scramble [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,17,3,707,94,5],"class_list":["post-67453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-internet-explorer","tag-scripting-guy","tag-shell-application","tag-special-folders","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I get a list of all my Internet cookies?&#8212; ZO 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 &#8211; and the word \u201ccookies\u201d &#8211; there was a mad scramble [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}