{"id":77653,"date":"2016-03-31T17:11:50","date_gmt":"2016-04-01T00:11:50","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=77653"},"modified":"2019-02-18T09:10:52","modified_gmt":"2019-02-18T16:10:52","slug":"use-powershell-5-0-to-create-temporary-files-in-the-temp-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-5-0-to-create-temporary-files-in-the-temp-folder\/","title":{"rendered":"Use PowerShell 5.0 to create temporary files in the temp folder"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell 5.0 on Windows 10 to create temporary files in the temporary folder.<\/p>\n<p>&nbsp;<\/p>\n<p>Sometimes it is the little things that make life easier. You know, like a cereal bar \u2026 it\u2019s not like a major technological breakthrough but it is much more convenient than getting a bowl of milk and opening a box of cereal and dumping it in the bowl. Or a peanut butter cup is easier to use than getting two bars of chocolate and a jar of peanut butter \u2026 neater too.<\/p>\n<p>Well in Windows PowerShell 5.0 there are lots of these stealth features that make life easier and simpler for me than even Windows PowerShell 4.0. I am talking about Windows PowerShell 5.0 on Windows 10, because the downlevel package will not have all the API\u2019s available for some of these way cool cmdlets.<\/p>\n<h2>Creating a temporary file in the temp folder<\/h2>\n<p>From time to time, I need to create a temporary file. In the old days I would create a file and give it a name that I would calculate. This wasn\u2019t random, but it worked pretty good if I tested to ensure that the file did not already exist, and if it did exist, then I would delete it and create the file. As you might surmise, that was about three lines of code \u2026 not as bad as in the VBScript days, but still it was boring to do. I even wrote my own create a temporary file function to relieve some of the tedium.<\/p>\n<p>Later, I was reading through the .NET Framework SDK and I came across a static method of the System.IO.Path class. It was called GetTempFileName. This was a bit confusing because at first everytime I called it to get a temporary file name, and then I used that temporary file name to create a temporary file, I would get an error \u2026 the call worked, but it generated an error. Later, I figured out that the GetTempFileName static method was actually misnamed, and should have been named CreateTempFile because that is what it does. I generates the temp file name AND creates the temporary file. So when I was calling the method to get the temp file name it created the file. Then when I attempted to use the temp file name to create the file \u2026 well an error occurred.<\/p>\n<h2>The really easy to create a temp file using PowerShell 5<\/h2>\n<p>The really easy to create a temp file is to use Windows PowerShell 5.0 on Windows 10 \u2026 because there is a New-TemporaryFile cmdlet. This is really simple:<\/p>\n<p>PS C:\\&gt; New-TemporaryFile<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Directory: C:\\Users\\mredw\\AppData\\Local\\Temp<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Mode\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LastWriteTime\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Length Name<\/p>\n<p>&#8212;-\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212;&#8212;&#8212;&#8212;-\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212;&#8212; &#8212;-<\/p>\n<p>-a&#8212;-\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\/31\/2016\u00a0\u00a0 8:05 PM\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0 tmp36BB.tmp<\/p>\n<p>&nbsp;<\/p>\n<p>Of course, this does not do a whole lot of good because while I have a temp file, I cannot really access it very easily because I don\u2019t know the name and path. Well, I can figure out the path perhaps, but not the file name. So the better way to do this is to capture the output. This appears here:<\/p>\n<p>PS C:\\&gt; $tmp = New-TemporaryFile<\/p>\n<p>$tmp | fl *<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>PSPath\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\mredw\\AppData\\Local\\Temp\\tmpBADC.tmp<\/p>\n<p>PSParentPath\u00a0\u00a0\u00a0\u00a0\u00a0 : Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\mredw\\AppData\\Local\\Temp<\/p>\n<p>PSChildName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : tmpBADC.tmp<\/p>\n<p>PSDrive\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : C<\/p>\n<p>PSProvider\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : Microsoft.PowerShell.Core\\FileSystem<\/p>\n<p>PSIsContainer\u00a0\u00a0\u00a0\u00a0 : False<\/p>\n<p>Mode\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : -a&#8212;-<\/p>\n<p>VersionInfo\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : File:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 C:\\Users\\mredw\\AppData\\Local\\Temp\\tmpBADC.tmp<\/p>\n<p>InternalName:<\/p>\n<p>OriginalFilename:<\/p>\n<p>FileVersion:<\/p>\n<p>FileDescription:<\/p>\n<p>Product:<\/p>\n<p>ProductVersion:<\/p>\n<p>Debug:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 False<\/p>\n<p>Patched:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 False<\/p>\n<p>PreRelease:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 False<\/p>\n<p>PrivateBuild:\u00a0\u00a0\u00a0\u00a0 False<\/p>\n<p>SpecialBuild:\u00a0\u00a0\u00a0\u00a0 False<\/p>\n<p>Language:<\/p>\n<p>&nbsp;<\/p>\n<p>BaseName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : tmpBADC<\/p>\n<p>Target\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : {}<\/p>\n<p>LinkType\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0:<\/p>\n<p>Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : tmpBADC.tmp<\/p>\n<p>Length\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0<\/p>\n<p>DirectoryName\u00a0\u00a0\u00a0\u00a0 : C:\\Users\\mredw\\AppData\\Local\\Temp<\/p>\n<p>Directory\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : C:\\Users\\mredw\\AppData\\Local\\Temp<\/p>\n<p>IsReadOnly\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : False<\/p>\n<p>Exists\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : True<\/p>\n<p>FullName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : C:\\Users\\mredw\\AppData\\Local\\Temp\\tmpBADC.tmp<\/p>\n<p>Extension\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : .tmp<\/p>\n<p>CreationTime\u00a0\u00a0\u00a0\u00a0\u00a0 : 3\/31\/2016 8:07:31 PM<\/p>\n<p>CreationTimeUtc\u00a0\u00a0 : 4\/1\/2016 12:07:31 AM<\/p>\n<p>LastAccessTime\u00a0\u00a0\u00a0 : 3\/31\/2016 8:07:31 PM<\/p>\n<p>LastAccessTimeUtc : 4\/1\/2016 12:07:31 AM<\/p>\n<p>LastWriteTime\u00a0\u00a0\u00a0\u00a0 : 3\/31\/2016 8:07:31 PM<\/p>\n<p>LastWriteTimeUtc\u00a0 : 4\/1\/2016 12:07:31 AM<\/p>\n<p>Attributes\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : Archive<\/p>\n<p>&nbsp;<\/p>\n<p>More than likely, I want the fullname property because it contains the path to the file as well as the file name and extension. It will be easy to use. This appears here:<\/p>\n<p>PS C:\\&gt; $tmp.FullName<\/p>\n<p>C:\\Users\\mredw\\AppData\\Local\\Temp\\tmpBADC.tmp<\/p>\n<p>&nbsp;<\/p>\n<p>So, now I can use Out-File to write to the file. This appears here:<\/p>\n<p>Get-Process | Out-File $tmp.FullName<\/p>\n<p>notepad $tmp.FullName<\/p>\n<p>&nbsp;<\/p>\n<p>Or, I can redirect to the file. This appears here:<\/p>\n<p>Get-Service &gt;&gt; $tmp.FullName<\/p>\n<p>notepad $tmp.FullName<\/p>\n<p>Remove-Item $tmp.FullName -Force<\/p>\n<p>&nbsp;<\/p>\n<p>When I am done, I want to use Remove-Item to delete the temporary file so that I keep things neat and tidy.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. Also check out my <a href=\"https:\/\/blogs.technet.microsoft.com\/msoms\/\">Microsoft Operations Management Suite Blog<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell 5.0 on Windows 10 to create temporary files in the temporary folder. &nbsp; Sometimes it is the little things that make life easier. You know, like a cereal bar \u2026 it\u2019s not like a major technological breakthrough but it is much more convenient than [&hellip;]<\/p>\n","protected":false},"author":599,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,673,674],"tags":[38,609,3,12,608,45],"class_list":["post-77653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-powershell-5","category-windows-10","tag-files","tag-powershell-5","tag-scripting-guy","tag-storage","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell 5.0 on Windows 10 to create temporary files in the temporary folder. &nbsp; Sometimes it is the little things that make life easier. You know, like a cereal bar \u2026 it\u2019s not like a major technological breakthrough but it is much more convenient than [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/77653","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\/599"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=77653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/77653\/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=77653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=77653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=77653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}