Home > Mobile >  PowerShell - Strip .exe from program call
PowerShell - Strip .exe from program call

Time:05-21

I really like the new PowerShell (7.2.4, in my case). However, I have to run certain programs that require a specific syntax:

$$ <prog> <ending>

<prog> is the name of the program, e.g. test, while the argument could be anything, e.g. this. The tricky part is that the program internally searches for a file that is called like test_this.cfg, rejecting its own name (test), the underscore and the file extension (.cfg). So the correct call would be:

$$ test this

This program was originally designed for a Linux environment, where the executable itself has no file extension on its own and therefore this call works. But now that I am on Windows, it has to be an .exe file, otherwise it is not being executed (Windows then asks what to do with this file). The PowerShell call then looks like this (the .\ are being added automatically by PowerShell):

$$ .\test this

but internally, the .exe extension seems to be there still, because the programm tells me that it cannot find a file called test.exe_this.cfg (of course it cannot, the name is test_this.cfg). Is there any way the PowerShell can simply ignore the .exe extension of the program, just like the default Windows CommandLine does?

Unfortunately, I'm not the program developer, so I cannot extend its functionalities to understand such patterns as well.

CodePudding user response:

No, because on Windows the file extension is still part of the file name. This is not a problem with PowerShell, it is simply how Windows execution works. The issue needs to be fixed in test.exe, the real problem is that test.exe has a bug on Windows when generating the file name that needs to be fixed.

CodePudding user response:

Bender the Greatest's helpful answer explains why a proper solution isn't possible without modifying the target program to account for the fact that executable files on Windows have filename extensions, typically .exe.

If you cannot modify the target program, here are potential workarounds:

  • Install WSL and directly run the Linux executable there; e.g.:

     wsl -e ./test this
    
  • Before running the Windows executable, create symlinks that effectively redirect file names such as test.exe_this.cfg to test_this.cfg

    • Note: Unless you have Developer Mode enabled in Windows, creating symlinks (symbolic links) requires elevation.

      # Create the symlink(s).
      Get-ChildItem test_*.cfg | ForEach-Object {
         New-Item -Type SymbolicLink ($_.Name -replace '^.[^_] ', '$&.exe') -Target $_.Name
      }
      
      # Now the target program should be able to find the right file
      # via its associated symlink.
      .\test this
      
  • Related