<p>I have a .ps1 file in which I want to define custom functions.</p>
<p>Imagine the file is called MyFunctions.ps1, and the content is as follows:</p>
<pre><code class="lang-auto">Write-Host "Installing functions"
function A1
{
Write-Host "A1 is running!"
}
Write-Host "Done"
</code></pre>
<p>To run this script and theoretically register the A1 function, I navigate to the folder in which the .ps1 file resides and run the file:</p>
<pre><code class="lang-auto">.\MyFunctions.ps1
</code></pre>
<p>This outputs:</p>
<pre><code class="lang-auto">Installing functions
Done
</code></pre>
<p>Yet, when I try to call A1, I simply get the error stating that there is no command/function by that name:</p>
<pre><code class="lang-auto">The term 'A1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
- A1 <<<<
- CategoryInfo : ObjectNotFound: (A1:String) [], CommandNotFoundException
- FullyQualifiedErrorId : CommandNotFoundException
</code></pre>
<p>I must misunderstand some PowerShell concepts. Can I not define functions in script files?</p>
<p><strong>Note</strong> that I have already set my execution policy to ‘RemoteSigned’. And I know to run .ps1 files using a dot in front of the file name: .\myFile.ps1</p>
<hr>
<p><em>Source : <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-5.1#using-dot-source-notation-with-scope" rel="noopener nofollow ugc">Stack Overflow</a></em></p>