Public/Invoke-Profile.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
function Invoke-Profile {
  <#
.SYNOPSIS
  Update the PowerShell Profile.
.DESCRIPTION
  Executes multiple functions defined in the TsekProfile module to install and
  configure the profile on the local system.
 
  Optionally, when this profile is being updated, you can hold SHIFT to
  display verbose messages.
.PARAMETER Force
  Controls whether or not the user is prompted to change directory.
.EXAMPLE
  profile.ps1 -Verbose
#>

  [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
  param (
    [switch]$Force
  )

  # Hold shift to turn on verbosity if running Windows PowerShell
  if ('Desktop' -eq $PSVersionTable.PSEdition) {
    Add-Type -Assembly PresentationCore, WindowsBase
    try {
      if ([System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::LeftShift) -OR
        [System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::RightShift)) {
        $VerbosePreference = "Continue"
      }
    }
    catch {
      Write-Warning 'Error displaying verbosity via SHIFT key.'
    }
  }

  try {
    Write-Verbose '==Removing default start up message=='
    Clear-Host

    Write-Verbose '==Setting custom prompt=='
    # TODO: Investigate passing switch parameter dynamically to this function.
    function global:prompt {
      Set-CustomPrompt -Force
    }

    Write-Verbose '==Checking console elevation=='
    Get-Elevation

    Write-Verbose '==Setting the console title=='
    Set-WindowTitle -Force:$Force

    Write-Verbose '==Setting the default directory for new PowerShell consoles=='
    Set-Path 'C:\Tmp' -Force:$Force

    Write-Verbose '==Installing fonts if necessary=='
    Install-Font

    Write-Verbose '==Changing to bash-like tab completion=='
    Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
    Set-PSReadLineOption -ShowToolTips -BellStyle Visual -HistoryNoDuplicates

    Write-Verbose '==Setting aliases=='
    Set-Alias -Name ll -Value Get-ChildItemColor -Scope Global -Option AllScope
    Set-Alias -Name ls -Value Get-ChildItemColorFormatWide -Scope Global -Option AllScope
    Set-Alias -Name History -Value Open-HistoryFile -Scope Global -Option AllScope

    Write-Verbose '==Getting and displaying list of helper functions=='
    Get-ExportedFunction
  }
  catch {
    throw "Error configuring `$profile on line $($_.InvocationInfo.ScriptLineNumber): $_"
  }
}