Many file extensions are bound to an executable application. That's why you can use Invoke-item to open a document.
It is not troublesome to find a file with a given suffix name that is opened by that default reference program. We can use the registry in the Windows system to programmatically resolve it. However, when scanning the registry, a little attention to the 32-bit and 64-bit machine problems, this is not the focus of this article.
Another way, slightly heterodoxy, calls the Windows API. The following example shows how to invoke the. The biggest advantage in taking this approach is the ability to borrow in the operating system. And your cost is only indirectly calling the functions in the Windows API with C # code:
$Source = @ "
using System;
Using System.Text;
Using System.Runtime.InteropServices;
public class Win32API
{
[DllImport (' Shell32.dll ', entrypoint= ' findexecutable ')] public
static extern Long Findexecutablea (String lpfile, String lpdirectory, StringBuilder lpresult);
public static string FindExecutable (String pv_strfilename)
{
StringBuilder objresultbuffer = new StringBuilder (1024);
Long lngresult = 0;
Lngresult = Findexecutablea (Pv_strfilename, String. Empty, Objresultbuffer);
if (Lngresult >=)
{return
objresultbuffer.tostring ();
}
return string. Format ("Error: ({0})", Lngresult);
}
" @
add-type-typedefinition $Source-erroraction silentlycontinue
$FullName = ' C:\Windows\windowsupdate.log '
$Executable = [Win32api]::findexecutable ($FullName)
' $FullName'll be launched by $Executable
The only limitation is that the file that findexecutable () needs to be checked is present, and you cannot request it only with the file name extension.
In addition, @reidca feedback said that the method could not detect files opened by an MMC add-in, such as a CER and a PFX certificate file, and the program would crash.