Public/FileSystem/Add-Font.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function Add-Font {
  <#
      #requires -Version 2.0
      .SYNOPSIS
      This will install Windows fonts.

      .DESCRIPTION
      Requries Administrative privileges. Will copy fonts to Windows Fonts folder and register them.

      .PARAMETER Path
      May be either the path to a font file to install or the path to a folder containing font files to install.
      Valid file types are .fon, .fnt, .ttf,.ttc, .otf, .mmm, .pbf, and .pfm

      .EXAMPLE
      Add-Font -Path Value
      Will get all font files from provided folder and install them in Windows.

      .EXAMPLE
      Add-Font -Path Value
      Will install provided font in Windows.


  #>




  [CmdletBinding(DefaultParameterSetName='Directory')]
  Param(
    [Parameter(Mandatory=$false,
      ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
    [Parameter(ParameterSetName='Directory')]
    [ValidateScript({Test-Path $_ -PathType Container })]
    [System.String[]]
    $Path,

    [Parameter(Mandatory=$false,
      ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
      [Parameter(ParameterSetName='File')]
    [ValidateScript({Test-Path $_ -PathType Leaf })]
    [System.String]
    $FontFile
  )

  begin {
    Set-Variable Fonts -Value 0x14 -Option ReadOnly
    $fontRegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"

    $shell = New-Object -ComObject Shell.Application
    $folder = $shell.NameSpace($Fonts)
    $objfontFolder = $folder.self.Path
    #$copyOptions = 20
    $copyFlag = [string]::Format("{0:x}",4+16)
    $copyFlag
  }

  process {
    switch ($PsCmdlet.ParameterSetName) {
      "Directory" {
        ForEach ($fontsFolder in $Path){
          Write-Log -Info -Message "Processing folder {$fontsFolder}"
          $fontFiles = Get-ChildItem -Path $fontsFolder -File -Recurse -Include @("*.fon", "*.fnt", "*.ttf","*.ttc", "*.otf", "*.mmm", "*.pbf", "*.pfm")
        }
      }
      "File" {
        $fontFiles = Get-ChildItem -Path $FontFile -Include @("*.fon", "*.fnt", "*.ttf","*.ttc", "*.otf", "*.mmm", "*.pbf", "*.pfm")
      }
    }
    if ($fontFiles) {
      foreach ($item in $fontFiles) {
        Write-Log -Info -Message "Processing font file {$item}"
        if(Test-Path (Join-Path -Path $objfontFolder -ChildPath $item.Name)) {
          Write-Log -Info -Emphasize -Message "Font {$($item.Name)} already exists in {$objfontFolder}"
        }
        else {
          Write-Log -Info -Emphasize -Message "Font {$($item.Name)} does not exists in {$objfontFolder}"
          Write-Log -Info -Message "Reading font {$($item.Name)} full name"

          Add-Type -AssemblyName System.Drawing
          $objFontCollection = New-Object System.Drawing.Text.PrivateFontCollection
          $objFontCollection.AddFontFile($item.FullName)
          $FontName = $objFontCollection.Families.Name

          Write-Log -Info -Message "Font {$($item.Name)} full name is {$FontName}"
          Write-Log -Info -Emphasize -Message "Copying font file {$($item.Name)} to system Folder {$objfontFolder}"
          $folder.CopyHere($item.FullName, $copyFlag)

          $regTest = Get-ItemProperty -Path $fontRegistryPath -Name "*$FontName*" -ErrorAction SilentlyContinue
          if (-not ($regTest)) {
            New-ItemProperty -Name $FontName -Path $fontRegistryPath -PropertyType string -Value $item.Name
            Write-Log -Info -Message "Registering font {$($item.Name)} in registry with name {$FontName}"
          }
        }
      }
    }
  }
  end {
  }
}