PowerShell Set File Type Association Default Application Windows 10

In our previous post we shared a command line tool to associate an application with a specific file type, however one of our visitors could not use the tool in his work environment due to restrictions because they could not use third-party executable applications, and told us that he wanted to do it using PowerShell.

Taking into account his very interesting idea and our passion for the world of scripting and sysadmin, we decided to create a simple version of SFTA in PowerShell using the Add-Type command that allows us to compile .NET code and use it in our Powershell instance.

##################################
##   __                         ##
##   |  \  _   _      _     _   ##
##   |__/ (_| | ) \/ _) \/ _)   ##
##                /     /       ##
##     © 2019 Danysys.com       ##
##       SFTA-PS v.1.0.0        ##
##################################

$sourceCSharp=@"
using System;
using System.Runtime.InteropServices;

namespace SFTA
{
    public static class AssocReg
    {


        [ComImport()]
        [Guid("14EBCC88-2831-4FC8-A5DF-9F36A81DB12C")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        private interface IApplicationAssociationRegistrationInternalW10_1709
        {
            [PreserveSig]
            void Dummy1();
            [PreserveSig]
            void SetProgIdAsDefault([MarshalAs(UnmanagedType.LPWStr)] string pszAppRegistryName, [MarshalAs(UnmanagedType.LPWStr)] string pszSet, int atSetType);
            [PreserveSig]
            void Dummy3();
            [PreserveSig]
            void Dummy4();
            [PreserveSig]
            void Dummy5();
            [PreserveSig]
            void Dummy6();
            [PreserveSig]
            void QueryCurrentDefault([MarshalAs(UnmanagedType.LPWStr)] string pszExt, int atQueryType, int alQueryLevel, [Out, MarshalAs(UnmanagedType.LPWStr)] out string ppszAssociation);
        }

        private interface IApplicationAssociationRegistrationInternalW8_1_63
        {
            [PreserveSig]
            void Dummy1();
            [PreserveSig]
            void SetProgIdAsDefault([MarshalAs(UnmanagedType.LPWStr)] string pszAppRegistryName, [MarshalAs(UnmanagedType.LPWStr)] string pszSet, int atSetType);
            [PreserveSig]
            void Dummy3();
            [PreserveSig]
            void Dummy4();
            [PreserveSig]
            void Dummy5();
            [PreserveSig]
            void Dummy6();
            [PreserveSig]
            void QueryCurrentDefault([MarshalAs(UnmanagedType.LPWStr)] string pszExt, int atQueryType, int alQueryLevel, [Out, MarshalAs(UnmanagedType.LPWStr)] out string ppszAssociation);
        }

        [ComImport()]
        [Guid("591209c7-767b-42b2-9fba-44ee4615f2c7")]
        [ClassInterface(ClassInterfaceType.None)]
        private class ApplicationAssociationRegistration
        {
        }

        public static bool Debug  = false;
        private static readonly IApplicationAssociationRegistrationInternalW10_1709 AppAssocReg10 = new ApplicationAssociationRegistration() as IApplicationAssociationRegistrationInternalW10_1709;
        private static readonly IApplicationAssociationRegistrationInternalW8_1_63 AppAssocReg8 = new ApplicationAssociationRegistration() as IApplicationAssociationRegistrationInternalW8_1_63;

        private static void WriteDebug(string message)
        {
            if ( Debug)
            {
                Console.WriteLine(DateTime.Now.ToString("[yyyy.dd.MM HH:mm:ss]") + " "+ message);
            }
        }
        public static void SetFileTypeAssociation(string ProgramId,string Ext)
        {
            if (AppAssocReg10 != null)
            {
                WriteDebug("OK AppAssocReg10");
            AppAssocReg10.SetProgIdAsDefault(ProgramId, Ext, 0);
                return;
            }

            if (AppAssocReg8 != null)
            {
                WriteDebug("OK AppAssocReg8");
            AppAssocReg8.SetProgIdAsDefault(ProgramId,Ext, 0);
                return;
            }

            WriteDebug("FAIL AppAssocReg");

        }

        public static string GetFileTypeAssociation( string Ext)
        {
            
            if (AppAssocReg10 != null)
            {
                WriteDebug("OK AppAssocReg10");
                var assocApplication = "";
                AppAssocReg10.QueryCurrentDefault(Ext, 0, 1, out assocApplication);
                WriteDebug("assocApplication = " + assocApplication);
                return assocApplication;
            }

            if (AppAssocReg8 != null)
            {
                WriteDebug("OK AppAssocReg8");
                var assocApplication = "";
                AppAssocReg8.QueryCurrentDefault(Ext, 0, 1, out assocApplication);
                WriteDebug("assocApplication = " + assocApplication);
                return assocApplication;
            }


            WriteDebug("FAIL AppAssocReg");
            return "";

        }

    }
}
"@
#Add Type
if (-not ([System.Management.Automation.PSTypeName]'SFTA.AssocReg').Type)
{
    Add-Type -TypeDefinition $sourceCSharp -Language CSharp
}


#Sample Usage
#[SFTA.AssocReg]::Debug=$true; #Show Debug Information
#Get
$assocProgram=[SFTA.AssocReg]::GetFileTypeAssociation(".pdf")
Write-Host(".PDF Assoc Program = " + $assocProgram)
#Set
[SFTA.AssocReg]::SetFileTypeAssociation("Applications\SumatraPDF.exe",".pdf")

We hope this will be useful for you!!!

Leave a Reply

Your email address will not be published. Required fields are marked *