Set Default File Association - Please Help

This topic contains 2 replies, has 2 voices, and was last updated by  H Man 2 years, 2 months ago.

  • Author
  • #36751

    H Man


    Im stuck for some reason that I dont know this is not working any advice on how to change default file associations would be highly appreciated.

    this is what I have so far... thank you

    [string]$extension = 'txt'
    [string]$executable = "C:\Program Files (x86)\Notepad++\notepad++.exe"
    if (-not (Test-Path $executable))
    	$errorMessage = "`'$executable`' does not exist, not able to create association"
    	throw $errorMessage
    $extension = $extension.trim()
    if (-not ($extension.StartsWith(".")))
    	$extension = ".$extension"
    $fileType = Split-Path $executable -leaf
    $fileType = $fileType.Replace(" ", "_")
    $elevated = @"
        cmd /c "assoc $extension=$fileType"
        cmd /c 'ftype $fileType="$executable" "%1" "%*"'
        New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
        Set-ItemProperty -Path "HKCR:\$fileType" -Name "(Default)" -Value "$fileType file" 
    -ErrorAction Stop
    iex $elevated
  • #37005

    Don Jones

    In your second Cmd, the string is delimited with single quotes, so won't do variable replacement. Only the outermost quotes count for this – the fact that you've used double quotes inside doesn't matter.

  • #37019

    H Man

    Thx Don!

You must be logged in to reply to this topic.