I have a ps1 script, which is launched via shortcut. Shortcut properties:
Target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -ExecutionPolicy Bypass -File "C:\Temp\myfile.ps1"
Start in: "C:\Temp"
Run as admin set true
Content of myfile.ps1:
. .\ps\file1.ps1
. .\ps\file2.ps1
Clear-Host
function1
function2
function1 is included in file1.ps1, function2 in file2.ps1
error-message:
function1: The term 'function1' 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 C:\Temp\myfile.ps1:1 char:1
+ function1
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (function1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Same for function2
in file2.ps1
If launched it via Powershell console, it works as intended. Probably sth is wrong / missing regarding the launch-in directory or similar?
Any hints are appreciated!
I cannot reproduce your problem, but I suggest changing your approach (as you've since confirmed, this fixes your problem):
I'm assuming you want to dot-source your helper .ps1
files from a location relative to the location of the executing script rather than relative to the current (working) directory.
$PSScriptRoot
variable contains the full path of the directory in which the executing scripts is located.Therefore, modify your script as follows:
# Dot-source the helper scripts from a subdirectory
# of the directory in which the running script is located.
. (Join-Path $PSScriptRoot ps\file1.ps1)
. (Join-Path $PSScriptRoot ps\file2.ps1)
Clear-Host
function1
function2