Last active
July 18, 2024 13:32
-
-
Save marckassay/f751c4ff681657f24fe3614e00c2396f to your computer and use it in GitHub Desktop.
This sample provides a script for IT pro or windows customers to quickly batch associated file extensions with application on Windows by PowerShell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
"This sample provides a script for IT pro or windows customers to quickly batch associated file extensions with application | |
on Windows by PowerShell." | |
.DESCRIPTION | |
"The most efficient way to change file associations is by selecting a default program. This tells Windows that you want a | |
certain program to be associated with all the file types it can handle. IT pro or windows customers want to have some kinds | |
of batch method to automatically associate a selection of file extensions. This sample script will help to quickly batch | |
the associated file extensions with application on Windows." | |
.PARAMETER FileExtensions | |
A string array of file extensions. Each extension must be prefixed with '.' | |
.PARAMETER OpenAppPath | |
Absolute path to executable. | |
.EXAMPLE | |
C:\> (New-Object Net.WebClient).DownloadString("https://gist.githubusercontent.com/marckassay/f751c4ff681657f24fe3614e00c2396f/raw/a3433e53eb8e427c5d8a8dee6a900b745b17dbf1/Set-AssociateFileExtensions.ps1") | iex | |
C:\> Set-AssociateFileExtensions -FileExtensions .txt,.sql -OpenAppPath "C:\Windows\System32\notepad.exe" | |
.NOTES | |
source: https://gallery.technet.microsoft.com/scriptcenter/How-to-associate-file-3898f323 | |
#> | |
Function Set-AssociateFileExtensions { | |
Param | |
( | |
[Parameter(Mandatory = $true)] | |
[String[]] $FileExtensions, | |
[Parameter(Mandatory = $true)] | |
[String] $OpenAppPath | |
) | |
if (-not (Test-Path $OpenAppPath)) { | |
throw "$OpenAppPath does not exist." | |
} | |
foreach ($extension in $FileExtensions) { | |
$fileType = (cmd /c "assoc $extension") | |
$fileType = $fileType.Split("=")[-1] | |
cmd /c "ftype $fileType=""$OpenAppPath"" ""%1""" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
assoc & ftype commands no longer works, because file type association is now protected by a secret hash in the registry ...
Here is a solution :
https://kolbi.cz/blog/2017/10/25/setuserfta-userchoice-hash-defeated-set-file-type-associations-per-user/
https://setuserfta.com
Hope this help