{"id":15691,"date":"2011-02-05T00:01:00","date_gmt":"2011-02-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/02\/05\/use-code-snippets-to-simplify-writing-powershell-scripts\/"},"modified":"2011-02-05T00:01:00","modified_gmt":"2011-02-05T00:01:00","slug":"use-code-snippets-to-simplify-writing-powershell-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-code-snippets-to-simplify-writing-powershell-scripts\/","title":{"rendered":"Use Code Snippets to Simplify Writing PowerShell Scripts"},"content":{"rendered":"<p><span style=\"font-size: x-small\"><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use code snippets to simplify writing Windows PowerShell scripts.<\/span><\/p>\n<h3><span style=\"font-size: x-small\">Weekend Scripter: Code snippets <\/span><\/h3>\n<p><span style=\"font-size: x-small\">Microsoft Scripting Guy, Ed Wilson, here. I was recently in a meeting in which Jeffrey Snover (the original architect for Windows PowerShell) was also present. I love hearing Jeffrey Snover talk because he is so enthusiastic, and also a genius. Anyway, during the meeting, he mentioned working with snippets, and he said it is a feature that could easily be added to the Windows PowerShell ISE. I quickly jotted the idea down in my meeting notes, and could not wait until I had the opportunity to try it out. <\/span><\/p>\n<p><span style=\"font-size: x-small\">I decided to use an instance of the <\/span><span style=\"font-size: x-small\"><a style=\"font-size: x-small\" href=\"\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.openfiledialog.aspx&quot;&gt;&lt;span style=\" target=\"_blank\">System.Windows.Forms.OpenFileDialog<\/a><\/span><span style=\"font-size: x-small\"> class that I had previously used in the <\/span><span style=\"font-size: x-small\"><a style=\"font-size: x-small\" href=\"http:\/\/blogs.technet.com\/controlpanel\/blogs\/posteditor.aspx\/&lt;a href=&quot;\/b\/heyscriptingguy\/archive\/2009\/09\/01\/hey-scripting-guy-september-1.aspx#comments&quot;&gt;&lt;span style=\" target=\"_blank\">Hey, Scripting Guy! Can I Open a File Dialog Box with Windows PowerShell?<\/a><\/span><span style=\"font-size: x-small\"> post. While I was at it, I took the opportunity to update the code to <\/span><a target=\"_blank\" href=\"http:\/\/support.microsoft.com\/kb\/968929\"><span style=\"font-size: x-small\">Windows PowerShell 2.0<\/span><\/a><span style=\"font-size: x-small\"> standards, which includes getting rid of the obsolete <b>LoadWithPartialName<\/b> static method from the <\/span><a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.reflection.assembly.aspx\"><span style=\"font-size: x-small\">System.Reflection.Assembly<\/span><\/a><span style=\"font-size: x-small\"> .NET Framework class, as well as using a <b>Try<\/b> \/ <b>Catch<\/b> construction to handle my error handling responsibilities. <\/span><\/p>\n<p><span style=\"font-size: x-small\">The complete <b>Get-CodeSnippet<\/b> function is shown here.<\/span><\/p>\n<blockquote>\n<p><span style=\"font-size: x-small\">Get-CodeSnippet.ps1<\/span><\/p>\n<p><span style=\"font-size: x-small\">Function Get-CodeSnippet<\/span><\/p>\n<p><span style=\"font-size: x-small\">{ <\/span><\/p>\n<p><span style=\"font-size: x-small\">&lt;#<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Synopsis<\/span><\/p>\n<\/blockquote>\n<blockquote>\n<p><span style=\"font-size: x-small\">Either displays contents of a file in Notepad, or adds the content to<\/span><\/p>\n<p><span style=\"font-size: x-small\">current insertion point of script.<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Description<\/span><\/p>\n<p><span style=\"font-size: x-small\">The Get-CodeSnippet function accepts an input folder of code snippets,<\/span><\/p>\n<p><span style=\"font-size: x-small\">and displays a file dialog box that allows you to select a particular<\/span><\/p>\n<p><span style=\"font-size: x-small\">file to either view the contents in Notepad, or add the content to <\/span><\/p>\n<p><span style=\"font-size: x-small\">your script at the current insertion point.<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Example<\/span><\/p>\n<p><span style=\"font-size: x-small\">Get-CodeSnippet -InitialDirectory c:\\fso<\/span><\/p>\n<p><span style=\"font-size: x-small\">Opens a File Dialog box situated on the c:\\fso directory. Initial view<\/span><\/p>\n<p><span style=\"font-size: x-small\">is .ps1 and .psm1 files, but you can choose to view all files. If you<\/span><\/p>\n<p><span style=\"font-size: x-small\">select a file, its contents will be displayed in Notepad.<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Example<\/span><\/p>\n<p><span style=\"font-size: x-small\">Get-CodeSnippet -InitialDirectory c:\\fso -add<\/span><\/p>\n<p><span style=\"font-size: x-small\">Opens a File Dialog box situated on the c:\\fso directory. Initial view<\/span><\/p>\n<p><span style=\"font-size: x-small\">is .ps1 and .psm1 files, but you can choose to view all files. If you<\/span><\/p>\n<p><span style=\"font-size: x-small\">select a file, its contents will automatically be added at the current<\/span><\/p>\n<p><span style=\"font-size: x-small\">insertion point of your script.<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Parameter InitialDirectory<\/span><\/p>\n<p><span style=\"font-size: x-small\">The initial directory to be homed for the Open File Dialog box<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Parameter Add<\/span><\/p>\n<p><span style=\"font-size: x-small\">A switched parameter that will cause the function to add the content to <\/span><\/p>\n<p><span style=\"font-size: x-small\">current script at current insertion point.<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Inputs <\/span><\/p>\n<p><span style=\"font-size: x-small\">[string]<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Outputs <\/span><\/p>\n<p><span style=\"font-size: x-small\">[string]<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Notes<\/span><\/p>\n<p><span style=\"font-size: x-small\">NAME: Get-CodeSnippet<\/span><\/p>\n<p><span style=\"font-size: x-small\">AUTHOR: ed wilson, msft<\/span><\/p>\n<p><span style=\"font-size: x-small\">LASTEDIT: 01\/26\/2011 13:14:07<\/span><\/p>\n<p><span style=\"font-size: x-small\">KEYWORDS: Scripting Techniques, Scripting Templates, Weekend Scripter<\/span><\/p>\n<p><span style=\"font-size: x-small\">HSG: WES-2-5-2011<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Link<\/span><\/p>\n<p><span style=\"font-size: x-small\">Http:\/\/www.ScriptingGuys.com<\/span><\/p>\n<p><span style=\"font-size: x-small\">#Requires -Version 2.0<\/span><\/p>\n<p><span style=\"font-size: x-small\">#&gt;<\/span><\/p>\n<p><span style=\"font-size: x-small\">Param(<\/span><\/p>\n<p><span style=\"font-size: x-small\">[Parameter(Mandatory=$true)]<\/span><\/p>\n<p><span style=\"font-size: x-small\">[string]$InitialDirectory,<\/span><\/p>\n<p><span style=\"font-size: x-small\">[switch]$add<\/span><\/p>\n<p><span style=\"font-size: x-small\">)<\/span><\/p>\n<p><span style=\"font-size: x-small\">If($ExecutionContext.Host.name -notmatch &#8220;ISE Host$&#8221;)<\/span><\/p>\n<p><span style=\"font-size: x-small\">{<\/span><\/p>\n<p><span style=\"font-size: x-small\">$response = Read-Host -Prompt &#8220;This script must run in Windows PowerShell ISE. &lt;Y&gt; to exit.&#8221;<\/span><\/p>\n<p><span style=\"font-size: x-small\">if($response -match &#8220;y&#8221;) { Exit }<\/span><\/p>\n<p><span style=\"font-size: x-small\">} #end if<\/span><\/p>\n<p><span style=\"font-size: x-small\">Add-Type -AssemblyName &#8220;System.windows.forms&#8221;<\/span><\/p>\n<p><span style=\"font-size: x-small\">$OpenFileDialog = new-object System.Windows.Forms.OpenFileDialog<\/span><\/p>\n<p><span style=\"font-size: x-small\">$OpenFileDialog.initialDirectory = $initialDirectory<\/span><\/p>\n<p><span style=\"font-size: x-small\">$OpenFileDialog.filter = &#8220;PowerShell Scripts (*.ps1;*.psm1)|*.ps1;*.psm1|All files (*.*)| *.*&#8221;<\/span><\/p>\n<p><span style=\"font-size: x-small\">$OpenFileDialog.ShowDialog() | out-null<\/span><\/p>\n<p><span style=\"font-size: x-small\">Try<\/span><\/p>\n<p><span style=\"font-size: x-small\">{<\/span><\/p>\n<p><span style=\"font-size: x-small\">if(!$add) <\/span><\/p>\n<p><span style=\"font-size: x-small\">{ notepad $openfileDialog.filename } <\/span><\/p>\n<p><span style=\"font-size: x-small\">if($add)<\/span><\/p>\n<p><span style=\"font-size: x-small\">{<\/span><\/p>\n<p><span style=\"font-size: x-small\">$content = Get-Content -Path $OpenFileDialog.filename -Encoding ascii -Delimiter `r`n<\/span><\/p>\n<p><span style=\"font-size: x-small\">$psise.CurrentFile.Editor.InsertText($content)<\/span><\/p>\n<p><span style=\"font-size: x-small\">} #end if $add<\/span><\/p>\n<p><span style=\"font-size: x-small\">} #end try<\/span><\/p>\n<p><span style=\"font-size: x-small\">Catch [System.Exception]<\/span><\/p>\n<p><span style=\"font-size: x-small\">{ &#8220;No file was selected.&#8221; }<\/span><\/p>\n<p><span style=\"font-size: x-small\">} #end function Get-CodeSnippet<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: x-small\">The first portion of the <b>Get-CodeSnippet<\/b> function contains the comment-based Help tags. The addition of this Help allows the function to interact with the <\/span><a target=\"_blank\" href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd347639.aspx\"><span style=\"font-size: x-small\">Get-Help cmdlet<\/span><\/a><span style=\"font-size: x-small\">. For more information about working with comment-based Help, see this Hey, Scripting Guy! post: <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/01\/07\/hey-scripting-guy-january-7-2010.aspx\"><span style=\"font-size: x-small\">How Do I Add Help Information for Windows PowerShell Parameters?<\/span><\/a><\/p>\n<p><span style=\"font-size: x-small\">The Help portion of the function is shown here.<\/span><\/p>\n<blockquote>\n<p><span style=\"font-size: x-small\">&lt;#<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Synopsis<\/span><\/p>\n<p><span style=\"font-size: x-small\">Either displays contents of a file in Notepad, or adds the content to<\/span><\/p>\n<p><span style=\"font-size: x-small\">current insertion point of script.<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Description<\/span><\/p>\n<p><span style=\"font-size: x-small\">The Get-CodeSnippet function accepts an input folder of code snippets,<\/span><\/p>\n<p><span style=\"font-size: x-small\">and displays a file dialog box that allows you to select a particular<\/span><\/p>\n<p><span style=\"font-size: x-small\">file to either view the contents in Notepad, or add the content to <\/span><\/p>\n<p><span style=\"font-size: x-small\">your script at the current insertion point.<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Example<\/span><\/p>\n<p><span style=\"font-size: x-small\">Get-CodeSnippet -InitialDirectory c:\\fso<\/span><\/p>\n<p><span style=\"font-size: x-small\">Opens a File Dialog box situated on the c:\\fso directory. Initial view<\/span><\/p>\n<p><span style=\"font-size: x-small\">is .ps1 and .psm1 files, but you can choose to view all files. If you<\/span><\/p>\n<p><span style=\"font-size: x-small\">select a file, its contents will be displayed in Notepad.<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Example<\/span><\/p>\n<p><span style=\"font-size: x-small\">Get-CodeSnippet -InitialDirectory c:\\fso -add<\/span><\/p>\n<p><span style=\"font-size: x-small\">Opens a File Dialog box situated on the c:\\fso directory. Initial view<\/span><\/p>\n<p><span style=\"font-size: x-small\">is .ps1 and .psm1 files, but you can choose to view all files. If you<\/span><\/p>\n<p><span style=\"font-size: x-small\">select a file, its contents will automatically be added at the current<\/span><\/p>\n<p><span style=\"font-size: x-small\">insertion point of your script.<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Parameter InitialDirectory<\/span><\/p>\n<p><span style=\"font-size: x-small\">The initial directory to be homed for the Open File Dialog box<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Parameter Add<\/span><\/p>\n<p><span style=\"font-size: x-small\">A switched parameter that will cause the function to add the content to <\/span><\/p>\n<p><span style=\"font-size: x-small\">current script at current insertion point.<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Inputs <\/span><\/p>\n<p><span style=\"font-size: x-small\">[string]<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Outputs <\/span><\/p>\n<p><span style=\"font-size: x-small\">[string]<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Notes<\/span><\/p>\n<p><span style=\"font-size: x-small\">NAME: Get-CodeSnippet<\/span><\/p>\n<p><span style=\"font-size: x-small\">AUTHOR: ed wilson, msft<\/span><\/p>\n<p><span style=\"font-size: x-small\">LASTEDIT: 01\/26\/2011 13:14:07<\/span><\/p>\n<p><span style=\"font-size: x-small\">KEYWORDS: Scripting Techniques, Scripting Templates, Weekend Scripter<\/span><\/p>\n<p><span style=\"font-size: x-small\">HSG: WES-2-5-2011<\/span><\/p>\n<p><span style=\"font-size: x-small\">.Link<\/span><\/p>\n<p><span style=\"font-size: x-small\">Http:\/\/www.ScriptingGuys.com<\/span><\/p>\n<p><span style=\"font-size: x-small\">#Requires -Version 2.0<\/span><\/p>\n<p><span style=\"font-size: x-small\">#&gt;<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: x-small\">Next, I add two parameters to the function. The first parameter, <i>InitialDirectory, <\/i>is used to specify the directory to search for code snippets, and it is a mandatory parameter. Therefore, it will cause a dialog box to display if it is missing. An example of such a dialog box is shown in the following image.<\/span><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/2543.WES-2-5-11-01_1F16E315.jpg\"><span style=\"font-size: x-small\"><img decoding=\"async\" height=\"146\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/5857.WES-2-5-11-01_thumb_4A7B040F.jpg\" alt=\"Image of dialog box\" border=\"0\" title=\"Image of dialog box\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/span><\/a><\/p>\n<p><span style=\"font-size: x-small\">The <i>Add<\/i> parameter is a switched parameter; therefore, it only takes effect when it is present. It is used to cause the function to add the selected code snippet to the current insertion point of the script. The parameter section of the script is shown here.<\/span><\/p>\n<blockquote>\n<p><span style=\"font-size: x-small\">Param(<\/span><\/p>\n<p><span style=\"font-size: x-small\">[Parameter(Mandatory=$true)]<\/span><\/p>\n<p><span style=\"font-size: x-small\">[string]$InitialDirectory,<\/span><\/p>\n<p><span style=\"font-size: x-small\">[switch]$add<\/span><\/p>\n<p><span style=\"font-size: x-small\">)<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: x-small\">The script next makes a check to determine if it is running within the Windows PowerShell ISE. If it is, everything is groovy; but if it is not running inside the Windows PowerShell ISE, a message appears that states the script needs to run in the ISE, and the script prompts to exit the host. This portion of the script is shown here.<\/span><\/p>\n<blockquote>\n<p><span style=\"font-size: x-small\">If($ExecutionContext.Host.name -notmatch &#8220;ISE Host$&#8221;)<\/span><\/p>\n<p><span style=\"font-size: x-small\">{<\/span><\/p>\n<p><span style=\"font-size: x-small\">$response = Read-Host -Prompt &#8220;This script must run in Windows PowerShell ISE. &lt;Y&gt; to exit.&#8221;<\/span><\/p>\n<p><span style=\"font-size: x-small\">if($response -match &#8220;y&#8221;) { Exit }<\/span><\/p>\n<p><span style=\"font-size: x-small\">} #end if<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: x-small\">Now, I add the <b>System.Windows.Forms<\/b> .NET Framework class to the Windows PowerShell ISE environment by using the <b>Add-Type<\/b> cmdlet. Next, I create an instance of the <b>OpenFileDialog<\/b> class, and I set the initial directory to that obtained via the input parameters. I use a filter that will display only .ps1 and .psm1 files. This portion of the script is shown here.<\/span><\/p>\n<blockquote>\n<p><span style=\"font-size: x-small\">Add-Type -AssemblyName &#8220;System.windows.forms&#8221;<\/span><\/p>\n<p><span style=\"font-size: x-small\">$OpenFileDialog = new-object System.Windows.Forms.OpenFileDialog<\/span><\/p>\n<p><span style=\"font-size: x-small\">$OpenFileDialog.initialDirectory = $initialDirectory<\/span><\/p>\n<p><span style=\"font-size: x-small\">$OpenFileDialog.filter = &#8220;PowerShell Scripts (*.ps1;*.psm1)|*.ps1;*.psm1|All files (*.*)| *.*&#8221;<\/span><\/p>\n<p><span style=\"font-size: x-small\">$OpenFileDialog.ShowDialog() | out-null<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: x-small\">I now use a <b>Try<\/b> \/ <b>Catch<\/b> construction to open the file that is selected in Notepad for viewing. If the <i>Add<\/i> parameter was used when the function was called, the code from the file is added at the current insertion point. This portion of the script is shown here.<\/span><\/p>\n<blockquote>\n<p><span style=\"font-size: x-small\">Try<\/span><\/p>\n<p><span style=\"font-size: x-small\">{<\/span><\/p>\n<p><span style=\"font-size: x-small\">if(!$add) <\/span><\/p>\n<p><span style=\"font-size: x-small\">{ notepad $openfileDialog.filename } <\/span><\/p>\n<p><span style=\"font-size: x-small\">if($add)<\/span><\/p>\n<p><span style=\"font-size: x-small\">{<\/span><\/p>\n<p><span style=\"font-size: x-small\">$content = Get-Content -Path $OpenFileDialog.filename -Encoding ascii -Delimiter `r`n<\/span><\/p>\n<p><span style=\"font-size: x-small\">$psise.CurrentFile.Editor.InsertText($content)<\/span><\/p>\n<p><span style=\"font-size: x-small\">} #end if $add<\/span><\/p>\n<p><span style=\"font-size: x-small\">} #end try<\/span><\/p>\n<p><span style=\"font-size: x-small\">Catch [System.Exception]<\/span><\/p>\n<p><span style=\"font-size: x-small\">{ &#8220;No file was selected.&#8221; }<\/span><\/p>\n<p><span style=\"font-size: x-small\">} #end function Get-CodeSnippet<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: x-small\">To use the <b>Get-CodeSnippet<\/b> function, I can add it to my <\/span><a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/05\/how-to-create-a-customizable-profile-for-the-powershell-ise.aspx\"><span style=\"font-size: x-small\">Windows PowerShell ISE Profile<\/span><\/a><span style=\"font-size: x-small\">, so that it is available, or I can run it once inside the Windows PowerShell ISE. At that point, it is added to the <b>Function: drive<\/b>, and I can call it directly from the immediate window.<\/span><\/p>\n<p><span style=\"font-size: x-small\">I now open a new Windows PowerShell ISE sheet, and type the function name in the immediate window, as shown in the following image.<\/span><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/1651.WES-2-5-11-02_68791203.jpg\"><span style=\"font-size: x-small\"><img decoding=\"async\" height=\"429\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/8507.WES-2-5-11-02_thumb_4C87DD0B.jpg\" alt=\"Image of PowerShell ISE sheet\" border=\"0\" title=\"Image of PowerShell ISE sheet\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/span><\/a><\/p>\n<p><span style=\"font-size: x-small\">I then select the code that I want to add to my new script from the <b>Open file<\/b> dialog box that appears, as shown in the following image.<\/span><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/3716.WES-2-5-11-03_434BA1CA.jpg\"><span style=\"font-size: x-small\"><img decoding=\"async\" height=\"366\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/1641.WES-2-5-11-03_thumb_39A33394.jpg\" alt=\"Image of dialog box\" border=\"0\" title=\"Image of dialog box\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/span><\/a><\/p>\n<p><span style=\"font-size: x-small\">When I click the <b>Open<\/b> button, the code that is contained inside the file is automatically added to my current script as shown in the following image.<\/span><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/5775.WES-2-5-11-04_44EC47D1.jpg\"><span style=\"font-size: x-small\"><img decoding=\"async\" height=\"429\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/4606.WES-2-5-11-04_thumb_627E22D0.jpg\" alt=\"Image of script example\" border=\"0\" title=\"Image of script example\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/span><\/a><\/p>\n<p><span style=\"font-size: x-small\">Of course, this technique is much more powerful than simply adding a single <\/span><a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394582(VS.85).aspx\"><span style=\"font-size: x-small\">WMI<\/span><\/a><span style=\"font-size: x-small\"> command. If the code snippet is designed to be easily incorporated into your current script, it can make things easy. I will expand on this topic tomorrow. <\/span><\/p>\n<p><span style=\"font-size: x-small\">I invite you to follow me on <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\"><span style=\"font-size: x-small\">Twitter<\/span><\/a><span style=\"font-size: x-small\"> and <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"font-size: x-small\">Facebook<\/span><\/a><span style=\"font-size: x-small\">. If you have any questions, send email to me at <\/span><a href=\"mailto:scripter@microsoft.com\"><span style=\"font-size: x-small\">scripter@microsoft.com<\/span><\/a><span style=\"font-size: x-small\">, or post your questions on the <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\"><span style=\"font-size: x-small\">Official Scripting Guys Forum<\/span><\/a><span style=\"font-size: x-small\">. See you tomorrow. Until then, peace.<\/span><\/p>\n<p><b><span style=\"font-family: Segoe UI;font-size: x-small\">Ed Wilson, Microsoft Scripting Guy<\/span><\/b><\/p>\n<p><span style=\"font-size: x-small\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use code snippets to simplify writing Windows PowerShell scripts. Weekend Scripter: Code snippets Microsoft Scripting Guy, Ed Wilson, here. I was recently in a meeting in which Jeffrey Snover (the original architect for Windows PowerShell) was also present. I love hearing Jeffrey Snover talk because he [&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,4,220,61,45],"class_list":["post-15691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-scripting-templates","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use code snippets to simplify writing Windows PowerShell scripts. Weekend Scripter: Code snippets Microsoft Scripting Guy, Ed Wilson, here. I was recently in a meeting in which Jeffrey Snover (the original architect for Windows PowerShell) was also present. I love hearing Jeffrey Snover talk because he [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15691","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=15691"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15691\/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=15691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}