MetaTrader5 forgets account details when started via powershell

 

I have a shortcut with these properties:
Target: "D:\Metatrader3\MetaQuotes\MetaTrader5\terminal64.exe" /portable
Start in: "D:\Metatrader3\MetaQuotes\MetaTrader5"

at this location: "D:\Metatrader3\MetaQuotes\MetaTrader5\portable.lnk"

When I double-click the shotcut, it opens the terminal and takes me to the account that I was logged in when I last closed the terminal.

I want to run a powershell script that monitors the terminal and restarts it if it crashes. Here is the script:

$dir ="Metatrader3"
$processName = "terminal64.exe"
$keyWord = "portable"

$query = "SELECT * FROM Win32_Process WHERE Name = '$processName' AND CommandLine LIKE '%$keyWord%' AND CommandLine LIKE '%$dir%' "

$results = Get-WmiObject -Query $query | measure
if ($results.Count -eq 0){
    $workDir = "D:\$($dir)\MetaQuotes\MetaTrader5"
    $filePath  = "$($workDir)\$($keyWord).lnk"

    Start-Process -FilePath $($filePath) -WorkingDirectory $($workDir)
}

Powershell opens the terminal, but the terminal has 'forgotten' what the last open account was. It takes me to the "Open account" screen instead of my charts.

I am struggling to figure out how does the double-click differ from invoking the shortcut programmaticaly. Does anyone know how the account information is invoked at terminal startup?

 
Viktoria Sudraba:

I have a shortcut with these properties:
Target: "D:\Metatrader3\MetaQuotes\MetaTrader5\terminal64.exe" /portable
Start in: "D:\Metatrader3\MetaQuotes\MetaTrader5"

at this location: "D:\Metatrader3\MetaQuotes\MetaTrader5\portable.lnk"

When I double-click the shotcut, it opens the terminal and takes me to the account that I was logged in when I last closed the terminal.

I want to run a powershell script that monitors the terminal and restarts it if it crashes. Here is the script:

Powershell opens the terminal, but the terminal has 'forgotten' what the last open account was. It takes me to the "Open account" screen instead of my charts.

I am struggling to figure out how does the double-click differ from invoking the shortcut programmaticaly. Does anyone know how the account information is invoked at terminal startup?

I believe you can pass your account credentials as you start the application from the terminal as command line arguments. 
 

Make sure your PowerShell script is running under the same Windows User account as when you work with MetaTrader manually.

Otherwise, it will just invalidate the encrypted trading account details.

 

I am not starting the app from the terminal, I am invoking a shortcut .Ink file - passing arguments to a shortcut does not make sense. And the PS script is running under the same (the only) account on my PC. But I appreciate the brainstorming. :)


Achieved the desired outcome by setting up a python script:

import os
def main(dir = 'Metatrader3'):
    shortcut = f'D:\\{dir}\\MetaQuotes\\MetaTrader5\\portable.lnk'
    os.startfile(shortcut)


if __name__ == '__main__':
    try:
        import sys

        main(sys.argv[1])
    except KeyboardInterrupt:
        pass

and starting that script via PS:


$processPath = "C:\Users\Viktoria\AppData\Local\Programs\Python\Python313\python.exe"

    if ($results.Count -eq 0) {

        $scriptPath = "D:\$($dir)\MetaQuotes\MetaTrader5\launch.py"
        $argList = "$($scriptPath) $($dir)"
        Start-Process $($processPath) -ArgumentList $($argList) -NoNewWindow:$false
    }

This works and does not forget the credentials.