{"id":17721,"date":"2010-07-18T00:01:00","date_gmt":"2010-07-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/07\/18\/hey-scripting-guy-weekend-scripter-how-can-i-create-display-and-then-delete-a-temporary-text-file\/"},"modified":"2010-07-18T00:01:00","modified_gmt":"2010-07-18T00:01:00","slug":"hey-scripting-guy-weekend-scripter-how-can-i-create-display-and-then-delete-a-temporary-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-how-can-i-create-display-and-then-delete-a-temporary-text-file\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: How Can I Create, Display, and Then Delete a Temporary Text File?"},"content":{"rendered":"<p>&nbsp;\nMicrosoft Scripting Guy Ed Wilson here, it&rsquo;s a good thing I enjoy getting up early on weekends. To me, because I get up early during the week, it makes sense to get up early on the weekend and get busy doing things that I want to do. One of the things I like to do early on weekend mornings is hang out on <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> for a little while as I wait for my <a href=\"http:\/\/en.wikipedia.org\/wiki\/English_Breakfast_tea\">English Breakfast tea<\/a> to steep, and as I wait for the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Irish_oatmeal\">Irish steel cut oats<\/a> to cook.\nOne of the things I wanted to look at this morning arose because of a presentation I made to the Philadelphia Area Exchange Users Group this week. I showed them a script I had written several years ago that creates a temporary text file, writes process information to the temporary text file, displays the information in Notepad, and then deletes the temporary text file when you close Notepad. It is a pretty cool script, and <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/a357dae8-9062-4422-af6a-340eb13280b7\">I posted it<\/a> to Scripting Guys Script Repository that night after the user group meeting. I thought the script would be more useful if I modified it to accept piped input. I got a little carried away, and turned it into an advanced function. The Out-TempFile.ps1 script contains the <strong>Out-TempFile<\/strong> advanced function. I added help tags so the function interacts with the <strong>Get-Help<\/strong> function. For more information about adding help to a <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/powershell.aspx\">Windows PowerShell<\/a> script, refer to the articles from <a href=\"http:\/\/blogs.technet.com\/search\/SearchResults.aspx?q=help&amp;sections=7618\">Help week<\/a>.\n<strong>Out-TempFile.ps1<\/strong>\n<strong><\/strong><\/p>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Out-TempFile<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">.Synopsis<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">Displays<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">pipelined<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">text<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">temporary<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">file.<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">The<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">temp<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">file<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">displayed<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad.<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">When<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">closed,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">temp<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">file<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">deleted.<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">If<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">you<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">wish<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">save<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">information<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">that<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">displayed<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">use<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">saveas<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">feature<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">from<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad.<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">.Example<\/span><span style=\"color: #808080\">     <br \/>&#8220;string&#8221; <\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Out-TempFile<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">Displays<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">word<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">string<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">temporary<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">file<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">via<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad.<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">When<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">closed,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">temporary<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">file<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">that<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">contained<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">word<\/span><span style=\"color: #808080\"> &#8220;string&#8221; <\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">deleted.<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">.Example<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #0000ff\">Get-Process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Out-TempFile<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">Displays<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">info<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">temporary<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">file<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">via<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad.<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">When<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">closed,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">temporary<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">file<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">that<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">contained<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">information<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">deleted.<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">.Example<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">Out-TempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">get-service<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">Displays<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">service<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">info<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">temporary<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">file<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">via<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad.<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">When<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">closed,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">temporary<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">file<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">that<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">contained<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">information<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">deleted.<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">.Parameter<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">Data<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">display<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">temporary<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">file<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">.Inputs<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">[psobject]<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">.Outputs<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">[psobject]<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">.Notes<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">NAME:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Out-TempFile<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">AUTHOR:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Ed<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Wilson<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">AUTHOR<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">BOOK:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Windows<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PowerShell<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Best<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Practices,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Microsoft<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Press<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2010<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">LASTEDIT:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">7<\/span><span style=\"color: #000000\">\/<\/span><span style=\"color: #800000\">15<\/span><span style=\"color: #000000\">\/<\/span><span style=\"color: #800000\">2010<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">WES-07-18-2010<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">KEYWORDS:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">WeekEnd<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Scripter,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Function,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Getting<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Started<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">.Link<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">Http:\/\/www.ScriptingGuys.com<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">Http:\/\/www.bit.ly\/HSGBlog<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">Requires<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Version<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">[CmdletBinding()]<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">param(<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">[Parameter(Mandatory<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$True<\/span><span style=\"color: #000000\">,valueFromPipeline<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #000000\">,Position<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #000000\">)]<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">[psobject]<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #2b91af\">$in<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">param<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">BEGIN<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$tempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[io.path]::GetTempFileName()<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">PROCESS<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">out-file<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-filePath<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$tempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-InputObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Append<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">-Width<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">300<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Encoding<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">unicode<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">END<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$tempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Out-Null<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #0000ff\">Remove-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$tempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">out-tempfile<\/span> <\/div>\n<p>After creating the help tags, I use the <strong>CmdletBinding<\/strong> attribute. This causes the function to bind parameters in the same way that a Windows PowerShell cmdlet binds the parameters. I then create the input parameter. To enable piped input to the function, it is necessary to specify the <strong>valueFromPipeLine<\/strong> parameter. The other parameters make the <strong>&ndash;in<\/strong> parameter mandatory and set it in the first position. The <strong>[psobject]<\/strong> type constraint makes the input an object, and not merely a string. The <strong>parameter<\/strong> section and <strong>CmdletBinding<\/strong> attribute are shown here:\n&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">[CmdletBinding()]<\/span><span style=\"color: #808080\">     <\/p>\n<p><\/span><span style=\"color: #000000\">param(<\/span><span style=\"color: #808080\">     <\/p>\n<p><\/span><span style=\"color: #000000\">[Parameter(Mandatory<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$True<\/span><span style=\"color: #000000\">,valueFromPipeline<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #000000\">,Position<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #000000\">)]<\/span><span style=\"color: #808080\">     <\/p>\n<p><\/span><span style=\"color: #000000\">[psobject]<\/span><span style=\"color: #808080\">     <\/p>\n<p><\/span><span style=\"color: #2b91af\">$in<\/span><span style=\"color: #808080\">     <\/p>\n<p><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">param<\/span> <\/div>\n<p>The first thing I want to do when the function is called is create the temporary file name. To do this, I use the <strong>GetTempFileName<\/strong> static method from the .NET Framework <strong>io.path <\/strong>class. I really do not need to know the temporary file name because I store it in the <strong>$tempFile<\/strong> variable. This variable will be used when creating and deleting the file. The <strong>Begin<\/strong> section of the advanced function defines actions that are performed once. This is shown here:\n&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">BEGIN<\/span><span style=\"color: #808080\">     <\/p>\n<p><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$tempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[io.path]::GetTempFileName()<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span> <\/div>\n<p>The <strong>Process<\/strong> section is run <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee198910.aspx\">for each<\/a> item that comes through the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\">pipeline<\/a>. The <strong>Out-File<\/strong> cmdlet is used to create the temporary text file and to append data to the file. I set the width to 300 characters and the encoding to Unicode. The Process block is shown here:\n&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">PROCESS<\/span><span style=\"color: #808080\">     <\/p>\n<p><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">out-file<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-filePath<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$tempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-InputObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Append<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\">     <\/p>\n<p><\/span><span style=\"color: #000000\">-Width<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">300<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Encoding<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">unicode<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span> <\/div>\n<p>There are two things that remain to be accomplished. The first is to display the temporary file in Notepad; this is very convenient because the user&rsquo;s temporary folder is cumbersome to navigate to. In addition, many programs have a nasty habit of leaving their temp files in the temp folder, and therefore spotting my specific temporary file gets to be difficult. It is much faster to have Notepad display it for me. After I am finished reading the file, I like to remove the temporary file. To do this, I pipe the Notepad command to the Out-Null cmdlet. This pauses execution of the script. When Notepad is closed, execution resumes and the <strong>Remove-Item<\/strong> cmdlet is used to delete the temporary file. The end section of the <strong>Out-TempFile<\/strong> function is shown here:\n&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">END<\/span><span style=\"color: #808080\">     <\/p>\n<p><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$tempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Out-Null<\/span><span style=\"color: #808080\">     <\/p>\n<p><\/span><span style=\"color: #0000ff\">Remove-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$tempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span> <\/div>\n<p>When the function is called, after you have run the script to load the function (in either the ISE or by dot-sourcing the script into the Windows PowerShell console), the function pauses execution while the temporary text file is displayed, as shown in the following image.\n<img decoding=\"async\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/july\/hey0718\/wes-07-18-10-01.jpg\" alt=\"Image of paused function as temporary text file is displayed\">\nJoin us tomorrow when we will begin looking at the <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/site\/search?f%5B0%5D.Type=RootCategory&amp;f%5B0%5D.Value=activedirectory&amp;f%5B0%5D.Text=Active%20Directory\">Active Directory<\/a> cmdlets that ship with Windows Server 2008 R2.\nWe would love for you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum.<\/a> See you tomorrow. Until then, peace.\n&nbsp;\n<strong><\/strong><\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here, it&rsquo;s a good thing I enjoy getting up early on weekends. To me, because I get up early during the week, it makes sense to get up early on the weekend and get busy doing things that I want to do. One of the things I like to [&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":[3,61,45],"class_list":["post-17721","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here, it&rsquo;s a good thing I enjoy getting up early on weekends. To me, because I get up early during the week, it makes sense to get up early on the weekend and get busy doing things that I want to do. One of the things I like to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17721","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=17721"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17721\/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=17721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}