Old-Lost

Get and Set File Associations

Apr 4th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Set-FileAssociation {
  2.     Param (
  3.         [Parameter(Mandatory)][string[]]$Extension,
  4.         [Parameter(Mandatory)][ValidateScript({ Test-Path $_ })][string]$Path
  5.     )
  6.     foreach ($e in $Extension) {
  7.         $e = $e -replace '\A\.?', '.'
  8.         $fileType = (cmd /c "assoc $e") -split '='
  9.         if ($fileType.Count -eq 2) {
  10.             $fileType = $fileType[1]
  11.             cmd /c "ftype $fileType=""$Path"" ""%1"""
  12.         } else {
  13.             Write-Error "Could not determine association for extension $e."
  14.         }
  15.     }
  16. }
  17.  
  18. function Get-FileAccociation {
  19.     param ([string]$FileType = '*')
  20.     cmd /c ftype | Select-String -Pattern '(.*?)=(.*)' | % { [pscustomobject]@{ FileType = $_.Matches[0].Groups[1].Value; Association = $_.Matches[0].Groups[2].Value } } | ? FileType -Like $FileType
  21. }
Advertisement
Add Comment
Please, Sign In to add comment