Public/Install-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
function Install-Profile {
  <#
  .SYNOPSIS
    Copy the TsekNet PowerShell profile to the appropriate location, and execute
    the profile script.
  .DESCRIPTION
    By default, overwrite the $profile.CurrentUserAllHosts file with the
    contents of the profile.ps1 file in this directory.
 
    Once the file is copied over, the profile script is executed.
  .Parameter Scope
    Name of the PowerShell profile scope to use.
    See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles for more information.
  .EXAMPLE
    Copy-Profile -Scope AllUsersAllHosts
 
    Overwrite the AllUsersAllHosts PowerShell Profile with the code in .
  #>

  [CmdletBinding()]
  param(
    [ValidateSet('AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost')]
    [string]$Scope = 'CurrentUserAllHosts'
  )

  $profile_dir = Split-Path $PROFILE.$Scope

  if (-not (Test-Path $profile_dir)) {
    $null = New-Item -Path $profile_dir -ItemType Directory
    Write-Verbose "Created new profile directory: $profile_dir"
  }

  Set-Content -Path $profile.CurrentUserAllHosts -Value 'Import-Module TsekProfile; Invoke-Profile -Force'
  Invoke-Profile -Force -Verbose
}