Dot sourcing in powershell

AATester 21 Reputation points

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


. 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.
4,745 questions
No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Andreas Baumgarten 67,616 Reputation points MVP

    Hi @AATester ,

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

    First script:

    # Script-file JustAtest.ps1 with function  
    function ABCDE{  
        param (  
        Write-Output $ComputerName  

    Second script calling the fist script:

    # Script Call-Script.ps1 calling another script "JustAtest.ps1" in the same folder  
    # relative path  
    ABCDE -ComputerName Computer12  
    # fullpath  
    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)

    Andreas Baumgarten

  2. Rich Matheisen 36,241 Reputation points

    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


  3. AATester 21 Reputation points

    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 37,526 Reputation points

    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–