{"id":73561,"date":"2015-09-04T00:01:00","date_gmt":"2015-09-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/09\/04\/adding-methods-to-a-powershell-5-class\/"},"modified":"2019-02-18T09:35:22","modified_gmt":"2019-02-18T16:35:22","slug":"adding-methods-to-a-powershell-5-class","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/adding-methods-to-a-powershell-5-class\/","title":{"rendered":"Adding Methods to a PowerShell 5 Class"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about adding methods to a Windows PowerShell 5.0 class in Windows 10.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Having a bunch of properties, although cool, is in the end, rather unsatisfying. I remember trying to teach VBScript and WMI in the old days. Although it was possible to glean lots of information, there were few things that actually did anything.<\/p>\n<p>I could easily find the name and the driver of the sound card, but I could not disable the sound card. I could find the screen resolution, but I could not change the screen resolution. I could find the currently logged on user, but I could not log the user out&#8230;and so on.<\/p>\n<p>It became very frustrating because in the end, it comes down to needing to actually do something. I mean I can describe all I want, but what can I really do? It is sort of the difference between reading a literary novel where nothing happens and reading a thriller where every page contains action. I prefer action&mdash;most of the time.<\/p>\n<p>In Windows PowerShell classes, it is the methods that actually do something. Properties describe things. So, how do I add a method?<\/p>\n<p>There are two types of methods I am going to add. For the first method, I simply call it, and it works. For the second method, I need to supply some information for the method to work. I am not adding any constraints or anything else. The point is to show you how to implement the methods. I will conclude the post by showing three ways of calling the methods after they are implemented.<\/p>\n<p><b>&nbsp; &nbsp;Note&nbsp;<\/b> This is the fifth post in a series about creating classes in Windows PowerShell&nbsp;5.0. You should read the earlier <br \/>&nbsp; &nbsp;posts prior to reading today&#039;s post:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/introduction-to-powershell-5-classes\/\" target=\"_blank\">Introduction to PowerShell 5 Classes<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-5-create-simple-class\/\" target=\"_blank\">PowerShell 5: Create Simple Class<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/adding-enums-to-powershell-5-classes\/\" target=\"_blank\">Adding Enums to PowerShell 5 Classes<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/creating-instances-of-powershell-5-classes\/\" target=\"_blank\">Creating Instances of PowerShell 5 Classes<\/a><\/li>\n<\/ul>\n<p><b>&nbsp; &nbsp;Another Note&nbsp;<\/b> I talked about enums last week. If you need to review enums, you should read:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/new-powershell-5-feature-enumerations\/\" target=\"_blank\">New PowerShell 5 Feature: Enumerations<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/working-with-enums-in-powershell-5\/\" target=\"_blank\">Working with Enums in PowerShell 5<\/a><\/li>\n<\/ul>\n<p>The first method I want to add does not require any input parameters. I simply call the method and it works. For an example, and to have easier to read code, I am going back to the basic <b>Car<\/b> class (without the enums):<\/p>\n<p style=\"margin-left:30px\"><strong>Class Ca<\/strong>r<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; &nbsp;[String]$vin<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; static [int]$numberOfWheels = 4<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [int]$numberOfDoors<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [datetime]$year<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$model<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>To add a method to the class, I simply go to the bottom of the class, and add the name of my method. I then use <b>()<\/b> to designate that it is a method, and I add my script block (surrounded by curly braces) that contains the code I want to call when I call the method.<\/p>\n<p>The first method I will add is called the <b>SoundHorn<\/b> method. When I call this method, it will sound a beep for a few seconds. The method is shown here:<\/p>\n<p style=\"margin-left:30px\">SoundHorn () {[console]::beep(800,900)}<\/p>\n<p>The next method I want to add is the <b>Accelerate<\/b> method. When I accelerate, I need to know how fast I am going, so I need to be able to pass a value to the method. The input will be contained in the <b>$arg<\/b> variable. I will then use this value to determine how fast my car will go.<\/p>\n<p>What the method is actually doing is starting at a relatively low beep, and increasing the frequency of the beep a certain number of times. This is determined by my speed<i> <\/i>supplied to the <b>$arg<\/b> variable. I do not have any error checking or parameter input validation, and so you should probably keep your speed less than 50 mph or so.<\/p>\n<p>Here is the method that accepts an input parameter:<\/p>\n<p style=\"margin-left:30px\">Accelerate ($arg)<\/p>\n<p style=\"margin-left:30px\">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{300..($arg * 100) |<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Foreach {[console]::beep($_,150)}}<\/p>\n<p>The complete&nbsp; class with the two new methods is shown here:<\/p>\n<p style=\"margin-left:30px\"><strong>Class Car<\/strong><\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$vin<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; static [int]$numberOfWheels = 4<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [int]$numberOfDoors<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [datetime]$year<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$model<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; SoundHorn () {[console]::beep(800,900)}<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Accelerate ($arg)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {300..($arg * 100) |<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Foreach {[console]::beep($_,150)}}<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>I run my script and I can test my methods. The first way I can call my method, is to create an instance of the car (a new <b>Car<\/b> object), store the returned object in a variable, and then call the method. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a = New-Object car<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a.SoundHorn()<\/p>\n<p>The second way to call the method is to call the <b>New<\/b> static method, and then call the method from that object:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [car]::new().SoundHorn()<\/p>\n<p>A third way to call the method uses <b>New-Object<\/b>, and it is what I call the &quot;group and dot.&quot; This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (New-Object car).SoundHorn()<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;&nbsp;<\/p>\n<p>To call the accelerate method, I can use the same techniques:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a = New-Object car<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a.Accelerate(25)<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [car]::new().Accelerate(50)<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (New-Object car).Accelerate(50)<\/p>\n<p>Here is the script and the associated calling of the methods:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-4-15-01.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-4-15-01.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to adding methods to a PowerShell 5.0 class. Windows PowerShell Classes Week will continue tomorrow when I will recap the class design and implementation.<\/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><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about adding methods to a Windows PowerShell 5.0 class in Windows 10. Microsoft Scripting Guy, Ed Wilson, is here. Having a bunch of properties, although cool, is in the end, rather unsatisfying. I remember trying to teach VBScript and WMI in the old days. Although it was possible [&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":[615,609,3,4,608,45],"class_list":["post-73561","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-classes","tag-powershell-5","tag-scripting-guy","tag-scripting-techniques","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about adding methods to a Windows PowerShell 5.0 class in Windows 10. Microsoft Scripting Guy, Ed Wilson, is here. Having a bunch of properties, although cool, is in the end, rather unsatisfying. I remember trying to teach VBScript and WMI in the old days. Although it was possible [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73561","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=73561"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73561\/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=73561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}