2

I'm trying to develop my own Boxstarter script for spinning up new machines. I just realized that I'd really like to add a line that will change default applications to open certain file types. For example, I want to open .txt files with Notepad++. I know how to do this by right-click the file and checking it's properties, but is there a line I can add to my Boxstarter script that will do it? Or, since Boxstarter is basically a special set of PowerShell commands, is there a PowerShell command I can invoke directly to change the opens with property? I did some searching, and most of the results were about how to get PowerShell to open something, not change the opens with property. The rest were all about how to open PowerShell.

tmoore82
  • 1,857
  • 1
  • 27
  • 48
  • 1
    Use powershell to amend registry: http://stackoverflow.com/questions/3924753/where-does-windows-store-its-open-with-settings – Raf Mar 07 '14 at 15:00

2 Answers2

2

Another similar, but not quite the same, way to go about this is to change the file association you want to associate with a particular applicaition. Chocolatey includes some helper commands to do this and is therefore available to your Boxstarter package. Here is an excerpt from one of my Boxstarter packages:

Install-ChocolateyFileAssociation ".txt" "$env:programfiles\Sublime Text 2\sublime_text.exe"
Install-ChocolateyFileAssociation ".dll" "$($Boxstarter.programFiles86)\jetbrains\dotpeek\v1.1\Bin\dotpeek32.exe"

So now double clicking on any text file opens sublime or any dll opens dotpeek.

But I agree. Its still helpful to be able to add to the "Open With..." list.

Matt Wrock
  • 6,590
  • 29
  • 23
  • Wow! That's a lot easier. And from the man himself! I just tried to run it from the Boxstarter Shell and got the error `The Term 'Install-ChocolateyFileAssociation not recognized... CommandNotFoundException`. I'm on version 2.3.24, running as Admin, execution policy unrestricted. Any thoughts as to why the command isn't recognized? – tmoore82 Mar 15 '14 at 14:25
  • 1
    Oh that is a chocolatey command that will not be automatically available in the boxstarter shell but is in any package install. If you entered 'Import-Module $env:chocolateyinstall\chocolateyinstall\helpers\chocolateyInstaller.psm1' then you could run the command in the shell. When a package is installing, that module is loaded in the installer's session. – Matt Wrock Mar 15 '14 at 16:14
0

Thanks to @Raf for pointing me in the right direction. Here's the code to change the OpensWith property of .txt files:

$principal = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice",[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::ChangePermissions)
$acl = $key.GetAccessControl()
$right = "SetValue"
$denyrule = New-Object System.Security.AccessControl.RegistryAccessRule($principal,$right,"DENY")
$ret = $acl.RemoveAccessRule($denyrule)
$ret = $key.SetAccessControl($acl)
Set-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice -Name ProgId -Value Applications\notepad++.exe

Slightly modified from an answer in the TechNet forums.

I haven't figured out if there's a boxstarter shortcut for this, but changing the ACL rules was the key. Without it, you don't have the proper access to change this particular registry item. Even when I tried running Powershell as Admin and made sure I had all the right permissions on the UserChoice key (both the administrator account and my user account had Full Control), I kept getting an error that the Requested registry access is not allowed.

tmoore82
  • 1,857
  • 1
  • 27
  • 48