{"id":3673,"date":"2013-05-08T00:01:00","date_gmt":"2013-05-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/05\/08\/i-found-this-powershell-functionnow-what-do-i-do-part-1\/"},"modified":"2013-05-08T00:01:00","modified_gmt":"2013-05-08T00:01:00","slug":"i-found-this-powershell-functionnow-what-do-i-do-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/i-found-this-powershell-functionnow-what-do-i-do-part-1\/","title":{"rendered":"I Found this PowerShell Function&#8212;Now What Do I Do? Part 1"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: A reader found needed code, but it is contained in a function. Microsoft Scripting Guy, Ed Wilson, provides the scoop about how to use functions.<\/span><\/p>\n<p align=\"left\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I have some experience with scripting&mdash;primarily VBScript script. I also have written a lot of batch files. But recently, I have begun to learn Windows PowerShell by reading your column and by watching some of your videos. What I am confused about is Windows PowerShell functions&mdash;I do not know what to do with them. Can you help?<\/p>\n<p>&mdash;JB<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello JB,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. We had a great SQL Server Week, and the Scripting Games are still going strong. The Scripting Wife has been doing well in the 2013 Scripting Games, and now she busy getting ready for TechEd 2013 in New Orleans. She is already planning excursions to get <a href=\"http:\/\/www.cafedumonde.com\/beignets\" target=\"_blank\">beignets<\/a> and <a href=\"http:\/\/www.auntsallys.com\/index.html\" target=\"_blank\">pralines<\/a>.<\/p>\n<p><span style=\"font-size: 12px\">There are several ways to use a Windows PowerShell function. The way you anticipate using a Windows PowerShell function will determine what you will do with the function after you have it. As an example, I am going to use the <\/span><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/2a631d72-206d-4036-a3f2-2e150f297515\" target=\"_blank\">Set-ScreenResolution function<\/a><span style=\"font-size: 12px\">, which was written by Andy Schneider. Teresa and I had a chance to catch up with Andy at the Windows PowerShell Summit where he gave a great talk for IT pros about source control. Here is a picture of Andy during his presentation.<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5165.hsg-5-8-13-1.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Photo of Andy Schneider\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5165.hsg-5-8-13-1.png\" alt=\"Photo of Andy Schneider\" \/><\/a><\/p>\n<p>Probably the easiest way to use a function is in a Windows PowerShell script, in much the same way that you used to use functions and subroutines back in the VBScript days. I will talk about this functionality today, and I will discuss the other methodologies next week.<\/p>\n<h2>Copy and paste the function directly into your script<\/h2>\n<p>When I go to the Scripting Guys Script repository, and I find Andy&rsquo;s <strong>Set-ScreenResolution<\/strong> function, all I need to do is use the control from the Script Repository and copy it to my clipboard. This technique is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2018.hsg-5-8-13-02.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2018.hsg-5-8-13-02.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>Therefore, if I want the &ldquo;run the script so it does stuff&rdquo; option, it is easiest to copy the function to the clipboard, open the Windows PowerShell ISE, and paste the function at the top. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1512.hsg-5-8-13-03.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of script\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1512.hsg-5-8-13-03.png\" alt=\"Image of script\" \/><\/a><\/p>\n<p>If I run my script right now, it will appear that nothing is happening because there is no code that calls the function. If I drop down to the interactive Windows PowerShell prompt (the bottom blue pane) and type the name of the function, I can actually call the function as shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4578.hsg-5-8-13-04.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of script\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4578.hsg-5-8-13-04.png\" alt=\"Image of script\" \/><\/a><\/p>\n<p>The solution is to add a line at the bottom of the script that contains the function that calls the function and passes values for the two required parameters: height and width. Here is the modification,<\/p>\n<p style=\"padding-left: 30px\">Set-ScreenResolution -Width 800 -Height 600<\/p>\n<p>This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5657.hsg-5-8-13-05.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of script\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5657.hsg-5-8-13-05.png\" alt=\"Image of script\" \/><\/a><\/p>\n<p>So now, if I run the script, it will automatically change the screen resolution on my primary monitor to 800&#215;600.<\/p>\n<h2>Running the script with the function<\/h2>\n<p>There are several ways to run the script that contains the <strong>Set-ScreenResolution<\/strong> function. Depending on your needs, you may decide one is better than the other.<\/p>\n<h3>Open in Windows PowerShell ISE and run it<\/h3>\n<p>The first way is to open the script in the Windows PowerShell ISE, and click the green triangle on the tool bar (press F5) to run the script. I use this technique in the following circumstances:<\/p>\n<ul>\n<li>When I am delivering a Windows PowerShell demo, and am showing a lot of scripts.<\/li>\n<li>When I am in the middle of writing a Windows PowerShell script.<\/li>\n<li>When I want to edit a script prior to running it (such as to change the width and height settings in the script).<\/li>\n<\/ul>\n<h3>Run from the Windows PowerShell console<\/h3>\n<p>To run the Set-ScreenResolution.ps1 script from the Windows PowerShell console, all I need to do is to pass the path to the script (note that there are no spaces in the name or path). This causes Windows PowerShell to execute the script as shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6076.hsg-5-8-13-06.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6076.hsg-5-8-13-06.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h3>Run from the Start\/Run command<\/h3>\n<p>I can also run a Windows PowerShell script by passing it as an argument to the <strong>Run<\/strong> command. (This is the same technique that I use when I use the Task Scheduler to run a Windows PowerShell script.) Here is the script:<\/p>\n<p style=\"padding-left: 30px\">powershell &#8220;C:\\data\\ScriptingGuys\\2013\\HSG_5_6_13\\Set-ScreenResolution.ps1&#8221;<\/p>\n<p>This is shown here in the <strong>Run<\/strong> dialog box.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4670.hsg-5-8-13-07.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of dialog box\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4670.hsg-5-8-13-07.png\" alt=\"Image of dialog box\" \/><\/a><\/p>\n<p>JB, that is all there is to using a function in a Windows PowerShell script.&nbsp; Join me tomorrow when I will reveal the 2013 Scripting Games Event&nbsp;3.<\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: A reader found needed code, but it is contained in a function. Microsoft Scripting Guy, Ed Wilson, provides the scoop about how to use functions. &nbsp;Hey, Scripting Guy! I have some experience with scripting&mdash;primarily VBScript script. I also have written a lot of batch files. But recently, I have begun to learn Windows PowerShell [&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":[69,3,4,45],"class_list":["post-3673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-functions","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: A reader found needed code, but it is contained in a function. Microsoft Scripting Guy, Ed Wilson, provides the scoop about how to use functions. &nbsp;Hey, Scripting Guy! I have some experience with scripting&mdash;primarily VBScript script. I also have written a lot of batch files. But recently, I have begun to learn Windows PowerShell [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3673","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=3673"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3673\/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=3673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}