{"id":70063,"date":"2005-04-08T19:29:00","date_gmt":"2005-04-08T19:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/04\/08\/how-can-i-get-a-list-of-all-the-pst-files-on-a-computer\/"},"modified":"2005-04-08T19:29:00","modified_gmt":"2005-04-08T19:29:00","slug":"how-can-i-get-a-list-of-all-the-pst-files-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-all-the-pst-files-on-a-computer\/","title":{"rendered":"How Can I Get a List of All the .PST Files on a Computer?"},"content":{"rendered":"<p><P>&nbsp;<\/P><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I get a list of all the .pst files on a computer &#8211; along with their size and their location &#8211; and then save that information to a comma-separated values file?<BR><BR>&#8212; DC<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, DC. If you hang around baseball fields long enough sooner or later you\u2019ll hear someone use the expression \u201ctailor-made.\u201d Typically what they are referring to is not some player\u2019s uniform but a ground ball that is doomed to be turned into a double play. Something that\u2019s tailor-made can\u2019t be anything <I>but<\/I> a double-play; for better or worse (depending on the team you\u2019re rooting for) the grounder was perfectly designed to be turned into two quick outs.<\/P>\n<P>Why do we mention this? (We mean besides the fact that at least one of the Scripting Guys spends far more time thinking about baseball than he does thinking about scripting.) Well, it turns out that this question is tailor-made for WMI: not only can WMI search any entire file system for any files that have a <B>.pst<\/B> file extension, but WMI can also retrieve folder information and file size as well. Tailor-made!<\/P>\n<P>Just to prove it, here\u2019s a script that retrieves information about all the .pst files on a computer. Note that this particular script displays information to the screen; we\u2019ll show you how to save this data to a comma-separated values (CSV) file a little later on:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_DataFile Where Extension = &#8216;pst'&#8221;)<\/p>\n<p>For Each objFile in colFiles\n    Wscript.Echo objFile.Drive &amp; objFile.Path\n    Wscript.Echo objFile.FileName &amp; &#8220;.&#8221; &amp; objFile.Extension\n    Wscript.Echo objFile.FileSize\n    Wscript.Echo\nNext\n<\/PRE>\n<P>Just a few things to point out here. After connecting to the WMI service we use <B>ExecQuery<\/B> to return a collection of all the files that have an <B>Extension<\/B> of <B>pst<\/B>. Note that we don\u2019t include the period in the file extension: it\u2019s <B>pst<\/B> rather than the <B>.pst<\/B>. That\u2019s just the way WMI does things.<\/P>\n<P>After retrieving this collection we create a For Each loop and echo the desired property values. This is a little bit tricky because of the way WMI stores naming information for a file. For example, suppose we have this file:<\/P><PRE class=\"codeSample\">c:\\documents and settings\\kmyer\\local settings\\application data\\microsoft\\outlook\\archive.pst\n<\/PRE>\n<P>The naming information is stored like this in WMI:<\/P>\n<TABLE class=\"dataTable\" id=\"EXD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>WMI Property<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Drive<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">C:<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Path<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">\\documents and settings\\kenmyer\\local settings\\application data\\microsoft\\outlook\\<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">FileName<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">archive<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Extension<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">pst<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>What does this mean? Well, it means that to display folder and files names the way we\u2019re used to seeing them we have to combine these property values before echoing them to the screen. To echo the folder name we combine the <B>Drive<\/B> and <B>Path<\/B> properties, like so:<\/P><PRE class=\"codeSample\">Wscript.Echo objFile.Drive &amp; objFile.Path\n<\/PRE>\n<P>To display the file name we combine the <B>FileName<\/B> and <B>Extension<\/B> properties:<\/P><PRE class=\"codeSample\">Wscript.Echo objFile.FileName &amp; &#8220;.&#8221; &amp; objFile.Extension\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"ENF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Yes, we could have done this instead:<\/P><PRE class=\"codeSample\">Wscript.Echo objFile.FileName &amp; &#8220;.pst&#8221;\n<\/PRE>\n<P>We took a more generic approach simply to show you how to construct file names in WMI.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>To display the file size we &#8211; oh, well, we just echo the value of the <B>FileSize<\/B> property. Hurray for FileSize, huh?<\/P>\n<P>Now, what about saving this data to a CSV file? We\u2019ll show you a script that does that; if you need more information about saving data to a CSV file take a look at this portion of the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_scr_hujq.mspx\" target=\"_blank\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>.<\/P>\n<P>Here\u2019s a script that retrieves information about .pst files and then saves that data to C:\\Scripts\\PST_Data.txt:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_DataFile Where Extension = &#8216;pst'&#8221;)<\/p>\n<p>If colFiles.Count = 0 Then\n    Wscript.Quit\nEnd If<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objTextFile = objFSO.CreateTextFile(&#8220;C:\\Scripts\\PST_Data.txt&#8221;)<\/p>\n<p>For Each objFile in colFiles\n    objTextFile.Write(objFile.Drive &amp; objFile.Path &amp; &#8220;,&#8221;)\n    objTextFile.Write(objFile.FileName &amp; &#8220;.&#8221; &amp; objFile.Extension &amp; &#8220;,&#8221;)\n    objTextFile.Write(objFile.FileSize &amp; vbCrLf)\nNext<\/p>\n<p>objTextFile.Close\n<\/PRE>\n<P>Notice how we write to the text file. We begin by writing the folder information followed by a comma. That gives us this:<\/P><PRE class=\"codeSample\">c:\\documents and settings\\kmyer\\local settings\\application data\\microsoft\\outlook\\,\n<\/PRE>\n<P>Next we add the file name, followed by another comma. Our text file will thus look like this:<\/P><PRE class=\"codeSample\">c:\\documents and settings\\kmyer\\local settings\\application data\\microsoft\\outlook\\,archive.pst,\n<\/PRE>\n<P>Finally we tack on the file size followed by a carriage-return linefeed (equivalent to pressing ENTER on the keyboard). We use the carriage return linefeed because we\u2019re done with the first .pst file and we want information for the next .pst file to be shown on a separate line. Our text file now looks like this:<\/P><PRE class=\"codeSample\">c:\\documents and settings\\kmyer\\local settings\\application data\\microsoft\\outlook\\,archive.pst,68741\n<\/PRE>\n<P>What\u2019d we tell you: tailor-made.<\/P>\n<P>Note that &#8211; depending on the size of your file system &#8211; this script might take 30 seconds or so to complete. If you know that your .pst files are likely to be found only on a particular drive or in a particular folder you might be able to speed that up a bit by modifying your WQL query. For example, this query searches only drive C: for .pst files:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_DataFile Where Extension = &#8216;pst&#8217; AND Drive = &#8216;C:'&#8221;)\n<\/PRE><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr05\/hey0408.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr05\/hey0408.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! How can I get a list of all the .pst files on a computer &#8211; along with their size and their location &#8211; and then save that information to a comma-separated values file?&#8212; DC Hey, DC. If you hang around baseball fields long enough sooner or later you\u2019ll hear someone use [&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":[38,3,4,12,5,6],"class_list":["post-70063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! How can I get a list of all the .pst files on a computer &#8211; along with their size and their location &#8211; and then save that information to a comma-separated values file?&#8212; DC Hey, DC. If you hang around baseball fields long enough sooner or later you\u2019ll hear someone use [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70063","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=70063"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70063\/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=70063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}