Dot sourcing in powershell

AATester 26 Reputation points
2022-04-12T18:57:55.153+00:00

Hi I have two ps1 files in a path C:\Users\myname\Documents\Files\P.ps1. I am trying to call a function 'launch' in P.ps1 into b.ps1. But, it throwing me error that The system cannot find the file specified C:\Users\myname\Documents\Files\P.ps1

b.ps1:

. C:\Users\myname\Documents\Files\P.ps1
launch -args $sys $prod

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,521 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Andreas Baumgarten 108.7K Reputation points MVP
    2022-04-12T19:59:30.807+00:00

    Hi @AATester ,

    If both scripts are in the same folder maybe this helps.

    First script:

    # Script-file JustAtest.ps1 with function  
    function ABCDE{  
        [CmdletBinding()]  
        param (  
            $ComputerName  
        )  
        Write-Output $ComputerName  
    }  
    

    Second script calling the fist script:

    # Script Call-Script.ps1 calling another script "JustAtest.ps1" in the same folder  
    # relative path  
    .\JustAtest.ps1  
    ABCDE -ComputerName Computer12  
      
    # fullpath  
    C:\Junk\JustAtest.ps1  
    ABCDE -ComputerName Computer13  
      
    # fullpath  
    & "C:\Junk\JustAtest.ps1"  
    ABCDE -ComputerName Computer14  
      
    # fullpath  
    . "C:\Junk\JustAtest.ps1"  
    ABCDE -ComputerName Computer15  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Rich Matheisen 46,711 Reputation points
    2022-04-13T02:02:30.46+00:00

    Are you sure the path is correct?

    . C:\Users\myname\Documents\Files\P.ps1

    I ran a PowerShell script (Untitled-22.ps1) with the current directory set to "C:\Junk" and in it I dot-sourced the file C:\Excel\myname\Documents\Files\P.ps1. Here's the script using the function ABCDE from @Andreas Baumgarten ):

    . C:\Excel\MyName\Documents\Files\P.ps1  
    ABCDE -ComputerName Computer12  
    

    This was the result:

    PS C:\junk> .\untitled-22.ps1

    Computer12

    0 comments No comments

  3. AATester 26 Reputation points
    2022-04-13T16:02:20.287+00:00

    Adding . .\Path\P.ps1 in b.ps1 worked for me when both files are in the same path. I ran b.ps1 directly from the path to pick up a function in P.ps1, it worked fine. Now the problem is that when I try to execute b.ps1 from javascript, it throws error that the b.ps1 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.


  4. Limitless Technology 39,611 Reputation points
    2022-04-19T07:41:17.05+00:00

    Hi there,

    It would be great if you share your script which would result in easier troubleshooting of your issue.

    Generally, if you are calling the function from a remote PC then you must Enable-PSRemoting on the remote machine where the script is executed.

    Enable-PSRemoting -Force

    Also, make sure you are executing the script with the right privileges and the account which you use for running scripts has all the permission to access the file that you are trying to call.

    In PowerShell, the Get-Acl command can be used to retrieve NTFS permissions reports. The script mentioned below helps retrieve the ACL set on the C:\commands folder.

    (Get-Acl -Path C:\commands).Access


    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.