How to trigger script using .bat from 'current directory' provided by input file

This topic contains 1 reply, has 2 voices, and was last updated by  Robbie Courtney 2 years ago.

  • Author
    Posts
  • #28737

    Sachin Ghagare
    Participant

    Hi All,

    I'm not able to execute PowerShell script successfully using .bat file provided by input file whereas it succeeds from PowerShell console/ISE

    Error: get-content : Cannot find path 'C:\Windows\system32\BulletinIDList.txt' because it does not exist.

    it seems script executes in "C:\Windows\system32\' context...how do I force script to look for input file in 'current directory'? please advise..!

    Below is script contents to read input from txt file,
    ——

    $currentFolder = $PWD.Path
    $GetBulletins = Join-Path $currentFolder "BulletinIDList.txt"
    $BulletinIDList = get-content $GetBulletins

    ——–

    Thanks

  • #28739

    Robbie Courtney
    Participant

    You need to use the $MyInvocation.MyCommand.Path to pull the correct directory that your script is running in. When you launch a PowerShell.exe by default it goes to the Path C:\Windows\system32. Your script is pulling that path when your are doing $PWD.Path
    Replace the following to pull the current location of the script.

    $currentFolder = Split-Path -Parent $MyInvocation.MyCommand.Path

You must be logged in to reply to this topic.