{"id":66203,"date":"2006-10-23T15:49:00","date_gmt":"2006-10-23T15:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/23\/how-can-i-use-windows-powershell-to-delete-all-the-tmp-files-on-a-drive\/"},"modified":"2006-10-23T15:49:00","modified_gmt":"2006-10-23T15:49:00","slug":"how-can-i-use-windows-powershell-to-delete-all-the-tmp-files-on-a-drive","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-windows-powershell-to-delete-all-the-tmp-files-on-a-drive\/","title":{"rendered":"How Can I Use Windows PowerShell to Delete All the .TMP Files on a Drive?"},"content":{"rendered":"<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 use Windows PowerShell to locate and delete all the .tmp files on a drive?<BR><BR>&#8212; OR<\/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, OR. You know, there\u2019s an old saying: Give a man a fish and he\u2019ll eat for a day; teach that man to fish and he\u2019ll eat for the rest of his life. <\/P>\n<P>Well, unless he\u2019s like the Scripting Guy who writes this column and doesn\u2019t like seafood. In that case he probably won\u2019t even <I>want<\/I> to learn to fish. And there\u2019s no point in giving him a fish, because he won\u2019t eat it. Instead, he\u2019ll end up starving to death and you\u2019ll spend the rest of your life wracked by guilt. \u201cWhy did I give him a fish?\u201d you\u2019ll think. \u201cWhy didn\u2019t I give him a hamburger or something? This is all my fault, isn\u2019t it?\u201d <\/P>\n<P>Yes, it is.<\/P>\n<P>The point is \u2013 well, to tell you the truth, we\u2019re no longer sure <I>what<\/I> the point is. Oh, now we remember. Can we show you how to use Windows PowerShell to locate and delete all the .tmp files on a drive? Of course we can. But then you\u2019d run a script for just one day. But if you <I>learned<\/I> Windows PowerShell then you could run scripts for the rest of your life. (You\u2019ll still be hungry, but at least you\u2019ll have something to take your mind off food.)<\/P>\n<P>In other words, if you\u2019re interested in Windows PowerShell then you owe it to yourself to attend <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/webcasts\/ps.mspx\"><B>Windows PowerShell Week<\/B><\/A>, November 6-10, 2006. This week is designed with one purpose in mind: to introduce you to Windows PowerShell. We\u2019ll have a series of webcasts (one each day, Monday through Friday); we\u2019ll have a virtual lab where you can try your hand at Windows PowerShell; and we\u2019ll have a few other assorted goodies (like a \u201clabcast,\u201d which is a combination virtual lab and webcast). It will be fun, it will be exciting, and, with any luck, it will be educational as well. We won\u2019t teach you how to fish, but we <I>will<\/I> teach you everything you need in order to get started with Windows PowerShell.<\/P>\n<P>Well, that\u2019s a good point: you never know <I>what<\/I> Peter will do during a webcast, do you? So maybe Peter will teach you to fish; we\u2019ll just have to wait and see.<\/P>\n<TABLE class=\"dataTable\" id=\"E3D\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Update: <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/webcasts\/ps.mspx\"><B>Windows PowerShell Week<\/B><\/A> is over, but you can watch the webcasts on-demand anytime.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So, other than fishing, what kinds of things will you learn during Windows PowerShell Week? Why, things like how to locate and delete all the .tmp files on a drive:<\/P><PRE class=\"codeSample\">get-childitem c:\\ -include *.tmp -recurse | foreach ($_) {remove-item $_.fullname}\n<\/PRE>\n<P>Believe it or not that <I>is<\/I> the entire command; as you can see, if you don\u2019t enjoy typing then Windows PowerShell is like a dream come true. What we have here are actually two commands crammed into one: we first retrieve a collection of all the .tmp files on drive C, then we go ahead and delete each of those files. <I>How<\/I> do we do that? Let\u2019s see if we can figure that out.<\/P>\n<P>To begin with, we use the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/msh\/cmdlets\/get-childitem.mspx\"><B>Get-ChildItem<\/B><\/A> Cmdlet to retrieve a collection of all the .tmp files on drive C. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">get-childitem c:\\ -include *.tmp -recurse\n<\/PRE>\n<P>This is actually fairly straightforward. We call Get-ChildItem (which, when working with the file system, functions somewhat similar to the dir command) and pass it three parameters:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>c:\\<\/B>, which represents the starting folder for our search.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>-include *.tmp<\/B>, which tells Get-ChildItem to return only files that have a .tmp file extension. Suppose we also wanted to include files that have a .temp file extension. That\u2019s fine; all we\u2019d have to do is add .temp to the list of included files: <B>-include *.tmp, *.temp<\/B>.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>-recurse<\/B>, which tells Get-ChildItem to look for files in all of the subfolders of C:\\ (and all the sub-subfolders of those subfolders, and so on). And yes, that <I>is<\/I> cool: you don\u2019t have to write some complicated recursive function to get at the files in the subfolders of a folder. Instead, just tack on the \u2013recurse parameter.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>After we\u2019ve retrieved our collection we \u201cpipe\u201d it over to the second half of our command (the | character represents the pipeline). Needless to say, we\u2019ll talk about piping in much more detail during Windows PowerShell Week. For now we\u2019ll just say that, by itself, Get-ChildItem typically retrieves a collection and then displays the returned information onscreen. However, we aren\u2019t interested in seeing the .tmp files displayed on screen; we want to delete those files. Therefore, instead of displaying the items on screen we ask Get-ChildItem to hand over (pipe) the entire collection to the second half of our command.<\/P>\n<P>In case you\u2019ve forgotten, the second half of our command looks like this:<\/P><PRE class=\"codeSample\">foreach ($_) {remove-item $_.fullname}\n<\/PRE>\n<P>What we\u2019re doing here is setting up a For Each loop to loop through the collection handed over by Get-ChildItem. Don\u2019t worry too much about the syntax for now; we\u2019ll discuss For Each loops in much more detail during Windows PowerShell Week. We will note, however, that the <B>$_<\/B> is a special Windows PowerShell variable that represents the individual items in the pipeline. When we say <B>foreach ($_)<\/B> that\u2019s equivalent to a VBScript statement like this:<\/P><PRE class=\"codeSample\">For Each objItem in colItems\n<\/PRE>\n<P>In other words, we\u2019re just looping through all the files in the collection.<\/P>\n<P>And what are we going to <I>do<\/I> to each of those files? Why we\u2019re going to use the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/msh\/cmdlets\/remove-item.mspx\"><B>Remove-Item<\/B><\/A> Cmdlet to delete them, of course:<\/P><PRE class=\"codeSample\">{remove-item $_.fullname}\n<\/PRE>\n<P>Here we call Remove-Item and pass, as the sole parameter, the <B>FullName<\/B> property of each individual file in the collection. (FullName is equivalent to the file path.) Believe it or not, that\u2019s all we have to do; Remove-Item takes care of the rest for us.<\/P>\n<P>What\u2019s that? You say this <I>sounds<\/I> cool, but, seeing as how you\u2019re new to Windows PowerShell, you\u2019re a little nervous about running a script that deletes a bunch of files? Hey, no problem. If that\u2019s the case, just tack the <B>\u2013whatif<\/B> parameter on to the Remove-Item command, like so:<\/P><PRE class=\"codeSample\">get-childitem c:\\ -include *.tmp -recurse | foreach ($_) {remove-item $_.fullname -whatif}\n<\/PRE>\n<P>That \u2013whatif parameter is pretty neat: instead of deleting the .tmp files it simply tells you what would happen if you really did ask Remove-Item to delete all the files. In other words, you\u2019ll get back a report of the files that would be deleted if you ran the command for real:<\/P><PRE class=\"codeSample\">What if: Performing operation &#8220;Remove File&#8221; on Target &#8220;C:\\WINDOWS\\SET3.tmp&#8221;.\nWhat if: Performing operation &#8220;Remove File&#8221; on Target &#8220;C:\\WINDOWS\\SET4.tmp&#8221;.\nWhat if: Performing operation &#8220;Remove File&#8221; on Target &#8220;C:\\WINDOWS\\SET8.tmp&#8221;.\n<\/PRE>\n<P>It\u2019s like fantasy football, only with Windows PowerShell.<\/P>\n<P>At any rate, we \u2013 oops, we need to go: Peter just walked by wearing hip waders and carrying a fishing pole. Admittedly, that\u2019s not all that unusual for Peter. But we better find out what he\u2019s up to, just in case.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use Windows PowerShell to locate and delete all the .tmp files on a drive?&#8212; OR Hey, OR. You know, there\u2019s an old saying: Give a man a fish and he\u2019ll eat for a day; teach that man to fish and he\u2019ll eat for the rest of his life. Well, [&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,12,45],"class_list":["post-66203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I use Windows PowerShell to locate and delete all the .tmp files on a drive?&#8212; OR Hey, OR. You know, there\u2019s an old saying: Give a man a fish and he\u2019ll eat for a day; teach that man to fish and he\u2019ll eat for the rest of his life. Well, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66203","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=66203"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66203\/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=66203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}