{"id":4821,"date":"2009-01-05T12:05:26","date_gmt":"2009-01-05T12:05:26","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/01\/05\/invoking-methods-using-variables\/"},"modified":"2019-02-18T13:12:57","modified_gmt":"2019-02-18T20:12:57","slug":"invoking-methods-using-variables","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/invoking-methods-using-variables\/","title":{"rendered":"Invoking Methods Using Variables"},"content":{"rendered":"<p>You can have a lot of fun with PowerShell.&#160; <br \/>Makes sense right &#8211; we&#8217;re busting our butts off and we get to define it so why wouldn&#8217;t we make it fun?    <br \/>Your busting your butts off at your job as well so why shouldn&#8217;t your tools be fun?&#160; Right?<\/p>\n<p>In particular, our late binding of names allows you to do some really cool, really powerful stuff.&#160; Here is an example:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">PS&gt; $d=Get-Date     <br \/>PS&gt; foreach ($p in &quot;Day&quot;,&quot;hour&quot;,&quot;minute&quot;) {&quot;$p :&#160; &quot; + $d.$p }      <br \/>Day :&#160; 5      <br \/>hour :&#160; 8      <br \/>minute :&#160; 53<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\"><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\"><\/font><\/p>\n<p>This allows you to write some very fun code.&#160; As soon as you start doing that, you&#8217;ll pretty quickly want to do the same thing for methods but that doesn&#8217;t work as well:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">PS&gt; foreach ($p in &quot;AddDays&quot;,&quot;AddHours&quot;,&quot;AddMinutes&quot;){&quot;$p :&#160; &quot; + $d.$p(1) } <\/font><\/p>\n<p><font color=\"#ff0000\" size=\"2\" face=\"Consolas\">Unexpected token &#8216;(&#8216; in expression or statement.     <br \/>At line:1 char:67 <\/font><\/p>\n<p><font color=\"#ff0000\" size=\"2\" face=\"Consolas\">Unexpected token &#8216;1&#8217; in expression or statement.     <br \/>At line:1 char:68 <\/font><\/p>\n<p><font color=\"#ff0000\" size=\"2\" face=\"Consolas\">Missing closing &#8216;}&#8217; in statement block.     <br \/>At line:1 char:69 <\/font><\/p>\n<p><font color=\"#ff0000\" size=\"2\" face=\"Consolas\">Unexpected token &#8216;)&#8217; in expression or statement.     <br \/>At line:1 char:69<\/font><\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>Here is how you make it work:&#160; you need to do is to specify the method as property and then call the Invoke() method on that property.&#160; It sounds a little strange so lets first review what happens when you specify a method as a property<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">PS&gt; <strong>$d.AddDays<\/strong> <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">MemberType&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : Method     <br \/>OverloadDefinitions : {System.DateTime AddDays(double value)}      <br \/>TypeNameOfValue&#160;&#160;&#160;&#160; : System.Management.Automation.PSMethod      <br \/>Value&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : System.DateTime AddDays(double value)      <br \/>Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : AddDays      <br \/>IsInstance&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : True <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">__________________________________________________________________________________________________________________________________     <br \/>PS&gt; <strong>$d.AddDays | Get-Member -View ALL i*<\/strong> <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">&#160;&#160; TypeName: System.Management.Automation.PSMethod <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">Name&#160;&#160;&#160;&#160;&#160;&#160; MemberType Definition&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#8212;-&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/><strong>Invoke<\/strong>&#160;&#160;&#160;&#160; Method&#160;&#160;&#160;&#160; System.Object Invoke(Params System.Object[] arguments)      <br \/>IsInstance Property&#160;&#160; System.Boolean IsInstance {get;}<\/font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>With that refresher &#8211; here is how you do it:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">PS&gt; <strong>foreach ($p in &quot;AddDays&quot;,&quot;AddHours&quot;,&quot;AddMinutes&quot;){&quot;$p :&#160; &quot; + $d.$p.<font color=\"#ff0000\">Invoke<\/font>(1) }<\/strong> <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">AddDays :&#160; 01\/06\/2009 08:53:53     <br \/>AddHours :&#160; 01\/05\/2009 09:53:53      <br \/>AddMinutes :&#160; 01\/05\/2009 08:54:53<\/font><\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>Enjoy!<\/p>\n<p>Jeffrey Snover [MSFT]   <br \/>Windows Management Partner Architect    <br \/>Visit the Windows PowerShell Team blog at:&#160;&#160;&#160; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a>    <br \/>Visit the Windows PowerShell ScriptCenter at:&#160; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can have a lot of fun with PowerShell.&#160; Makes sense right &#8211; we&#8217;re busting our butts off and we get to define it so why wouldn&#8217;t we make it fun? Your busting your butts off at your job as well so why shouldn&#8217;t your tools be fun?&#160; Right? In particular, our late binding of [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4821","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>You can have a lot of fun with PowerShell.&#160; Makes sense right &#8211; we&#8217;re busting our butts off and we get to define it so why wouldn&#8217;t we make it fun? Your busting your butts off at your job as well so why shouldn&#8217;t your tools be fun?&#160; Right? In particular, our late binding of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4821"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4821\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}