Public/New-Shortcut.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
function New-Shortcut{
  param(
    [Parameter(Mandatory)]
    [ValidateScript({
          Test-Path -Path $_
    })]
    [String]$Target,
    [Parameter(Mandatory)]
    [ValidateScript({
          Test-Path -Path $_ -IsValid
    })]
    [ValidateScript({
          if( -not (Test-Path -Path $_)) {
            Throw ('{0} already exists' -f $_)
          }
          if( -not ($_ -match '[.lnk]')) {
            Throw ('{0} must end in .lnk' -f $_)
          }
          return $true
    })]
    [String]$ShortcutFile,
    [switch]$RunAsAdmin
  )
  begin{
    $WScriptShell = New-Object -ComObject WScript.Shell
  }
  process{
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
    $Shortcut.TargetPath = $Target
    $Shortcut.Save()
    Write-Verbose -Message 'Shortcut Saved' -Verbose:$VerbosePreference

    if($RunAsAdmin -eq $True) {
      $bytes = [System.IO.File]::ReadAllBytes($ShortcutFile)
      $bytes[0x15] = $bytes[0x15] -bor 0x20
      [System.IO.File]::WriteAllBytes($ShortcutFile, $bytes)
      Write-Verbose -Verbose:$VerbosePreference -Message ('{0} - Set to Run as Admin' -f $ShortcutFile)
    }
  }
}