{"id":74161,"date":"2015-08-05T00:01:00","date_gmt":"2015-08-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/08\/05\/create-temporary-files-with-powershell-5\/"},"modified":"2019-02-18T09:46:43","modified_gmt":"2019-02-18T16:46:43","slug":"create-temporary-files-with-powershell-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/create-temporary-files-with-powershell-5\/","title":{"rendered":"Create Temporary Files with PowerShell 5"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about creating temporary files with Windows PowerShell 5.0 in Windows 10.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a nice Gunpowder Green tea with hibiscus leaves and rose hips in it. I also added a bit of lemon grass for taste. I made the tea last night and chilled it. The taste is surprisingly clean and refreshing. Along with some Belgium crisp chocolate sticks, it makes a delightful mid-morning snack.<\/p>\n<p>I am stilling playing around with Windows 10 and Windows PowerShell 5.0. I suspect this will probably be a task that will occupy me until the next versions ship. One thing I know I to have to do is clean up my profiles. The reason? Well, many of the things I had written (like the ISE Transcript function) are now included in Windows PowerShell 5.0, for example, the ISE Transcript function (see <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-transcript-tool-in-powershell-ise\/\" target=\"_blank\">Use Transcript Tool in PowerShell ISE<\/a>).<\/p>\n<p>In July 2010, I wrote a function I called <b>Out-TempFile<\/b> (see <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-how-can-i-create-display-and-then-delete-a-temporary-text-file\/\" target=\"_blank\">How Can I Create, Display, and Then Delete a Temporary Text File?<\/a>) I could use it to create temporary files. One thing that function did that the <b>New-TemporaryFile <\/b>cmdlet doesn&rsquo;t do is allow me to pipe directly to a temporary file. In addition, when I was done, it would automatically delete the temporary file. So I may keep it around and update it to use <b>New-TemporaryFile<\/b>.<\/p>\n<p>In Windows PowerShell 5.0, there is a new cmdlet called <b>New-TemporaryFile<\/b>. It will create a new temporary file in the temporary file location. It also returns a <b>FileInfo<\/b> object, so I can easily pick up the complete path to the file. All I need to do is to capture the return from the cmdlet. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">$tmp = New-TemporaryFile<\/p>\n<p>To get the complete path to the temporary file, I use the <b>FullName<\/b> property:<\/p>\n<p style=\"margin-left:30px\">$tmp.FullName<\/p>\n<p>If I want to output to the temporary file, I can pipe to the <b>Out-File<\/b> cmdlet, and pass the <b>FullName<\/b> parameter, like this:<\/p>\n<p style=\"margin-left:30px\">Get-Process | Out-File $tmp.FullName<\/p>\n<p>These commands are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-8-5-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-8-5-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>It is really important to capture the output from <b>New-TemporaryFile<\/b> cmdlet because it is basically a random file name, and therefore, it can be a major pain to find&mdash;even supposing I can remember where the temporary file storage is located. Also, this makes it easy for me to view the temp file because I can use Notepad and pass the <b>$tmp.FullName<\/b> command to it. As shown here, when I do, the output appears in Notepad:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-8-5-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-8-5-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>When I am done, I like to clean up. Therefore, I do not want to leave the temporary file lying around. Once again, I use the <b>FullName<\/b> property from the <b>$tmp<\/b> variable:<\/p>\n<p style=\"margin-left:30px\">Remove-Item $tmp.FullName -Force<\/p>\n<p>That is all there is to using Windows PowerShell 5.0 to create and to delete temporary files. Join me tomorrow when I&#039;ll continue talking about way cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about creating temporary files with Windows PowerShell 5.0 in Windows 10. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a nice Gunpowder Green tea with hibiscus leaves and rose hips in it. I also added a bit of lemon grass for taste. I made [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[609,3,608,45],"class_list":["post-74161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powershell-5","tag-scripting-guy","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about creating temporary files with Windows PowerShell 5.0 in Windows 10. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a nice Gunpowder Green tea with hibiscus leaves and rose hips in it. I also added a bit of lemon grass for taste. I made [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74161","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=74161"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74161\/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=74161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}