{"id":4421,"date":"2009-03-01T12:37:04","date_gmt":"2009-03-01T12:37:04","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/03\/01\/where-did-that-come-from\/"},"modified":"2019-02-18T13:12:47","modified_gmt":"2019-02-18T20:12:47","slug":"where-did-that-come-from","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/where-did-that-come-from\/","title":{"rendered":"Where Did That Come From?"},"content":{"rendered":"<p>Have you ever found yourself asking the question, &quot;where did THAT come from?&quot;?<\/p>\n<p>In PowerShell we try to give you mechanisms you can use to answer questions like that.&#160;&#160;&#160; We spend extra resources (memory, cpu cycles, etc) to do this because we focus in the performance of the PEOPLE using our software instead of the performance of the code.&#160; When something goes pear-shaped, what matters is how quickly a person can figure out what is going on and fix it.&#160; <\/p>\n<p>We&#8217;ve talked quite&#160; bit about this in the context of the rich error information we gather but in this blog I&#8217;m going to discuss another V1 feature and then tell you about ANOTHER one of my favorite V2isms.&#160; <\/p>\n<p>You know that variables are exposed as a drive and you can do things like:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">PS&gt; dir Variable:s* <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">Name&#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; Value     <br \/>&#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; &#8212;&#8211;      <br \/>ShellId&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Microsoft.PowerShell<\/font><\/p>\n<p>&#160;<\/p>\n<p>Bug have you have tried this:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">PS&gt; dir Variable:s* |ft Name,<strong><font color=\"#ff0000\">Description<\/font><\/strong> -auto <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">Name&#160;&#160;&#160;&#160;&#160;&#160; Description     <br \/>&#8212;-&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;&#8212;&#8211;      <br \/>ShellId&#160;&#160;&#160; The ShellID identifies the current shell.&#160; This is used by #Requires.      <br \/>StackTrace<\/font><\/p>\n<p>&#160;<\/p>\n<p>Did you know about that?&#160; There is a lot more to variables than meets the eye!&#160; You can provide a description with a variable by using Set-Variable<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">PS&gt; Set-Variable X 15 -Description &quot;This is a test&quot;     <br \/>PS&gt; Get-Variable x |fl * <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">Name&#160;&#160;&#160;&#160;&#160;&#160;&#160; : x     <br \/>Description : This is a test      <br \/>Value&#160;&#160;&#160;&#160;&#160;&#160; : 15      <br \/>Visibility&#160; : Public      <br \/>Module&#160;&#160;&#160;&#160;&#160; :      <br \/>ModuleName&#160; :      <br \/>Options&#160;&#160;&#160;&#160; : None      <br \/>Attributes&#160; : {}<\/font><\/p>\n<p>&#160;<\/p>\n<p>That has existed since PowerShell V1 but what we never were able to answer is &#8211; where did that <strong>function<\/strong> come from?&#160; <\/p>\n<p>When I start PowerShell, I dot-source a number of scripts which define functions as part of my session.&#160; Occasionally, I&#8217;ll decide that I want a function to do something different.&#160; But then the question is, what file was the function defined in?&#160; There was never a good way to answer that question.&#160; That all changes in PowerShell V2.<\/p>\n<p>In V2, we have extended the SCRIPTBLOCK object to include the FILE where the script was defined.&#160; One of the tricks you need to learn is that to get all the information about a Scriptblock, you need to use the -FORCE flag on the formatting commands.<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">[4376:0]PS&gt; (dir function:prompt ).scriptblock |fl * <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; if ($PSDebugContext)     <br \/>&#160;&#160;&#160; { &quot;DBG&gt;&quot;      <br \/>&#160;&#160;&#160; }      <br \/>&#160;&#160;&#160; else      <br \/>&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;[{0}:{1}]PS&gt; &quot; -f $PID, $NestedPromptLevel      <br \/>&#160;&#160;&#160; } <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">[4376:0]PS&gt; (dir function:prompt ).scriptblock |fl * <strong><font color=\"#ff0000\">-force<\/font><\/strong> <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">IsFilter&#160;&#160;&#160;&#160;&#160; : False     <br \/>StartPosition : System.Management.Automation.PSToken      <br \/><strong><font color=\"#ff0000\">File&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : D:\\ps\\profile.ps1         <br \/><\/font><\/strong>Attributes&#160;&#160;&#160; : {}      <br \/>Module&#160;&#160;&#160;&#160;&#160;&#160;&#160; :<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><\/font><\/p>\n<p>Now that you know it is there, you can just access it directly:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">[4376:0]PS&gt; (dir function:prompt ).scriptblock.file     <br \/>D:\\ps\\profile.ps1<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><\/font><\/p>\n<p>PSMDTAG:FAQ: How do I tell where a function was defined?   <br \/>(dir function:X).ScriptBlock.File<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>Experiment! Enjoy! Engage! <\/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>Have you ever found yourself asking the question, &quot;where did THAT come from?&quot;? In PowerShell we try to give you mechanisms you can use to answer questions like that.&#160;&#160;&#160; We spend extra resources (memory, cpu cycles, etc) to do this because we focus in the performance of the PEOPLE using our software instead of the [&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-4421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Have you ever found yourself asking the question, &quot;where did THAT come from?&quot;? In PowerShell we try to give you mechanisms you can use to answer questions like that.&#160;&#160;&#160; We spend extra resources (memory, cpu cycles, etc) to do this because we focus in the performance of the PEOPLE using our software instead of the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4421","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=4421"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4421\/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=4421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}