PowerShell: Recreate LastPass Desktop Shortcut
[CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="None")]
PARAM(
[switch]$AllUsers
)
begin {
# Adminsitrator role check
function IsLocalAdministrator() {
$CurrentWindowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$CurrentWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($CurrentWindowsIdentity)
$IsInWindowsBuiltInAdministratorRole = $CurrentWindowsPrincipal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
return $IsInWindowsBuiltInAdministratorRole
}
try {
$WshShell = New-Object -ComObject WScript.Shell
} catch {
$_
return
}
$InstallDir = (Get-Item -Path "$env:PROGRAMFILES\LastPass\lastpass.exe" -ErrorAction SilentlyContinue | Select "Directory" -ErrorAction SilentlyContinue).Directory.FullName
if (!$InstallDir) {
$InstallDir = (Get-Item -Path "$env:PROGRAMFILES (x86)\LastPass\lastpass.exe" -ErrorAction SilentlyContinue | Select "Directory" -ErrorAction SilentlyContinue).Directory.FullName
}
if (!$InstallDir) {
# Fake it
$InstallDir = "$env:PROGRAMFILES (x86)\LastPass"
}
}
process {
$BaseFileName = "My LastPass Vault"
$BaseFolder = [Environment]::GetFolderPath("Desktop")
if ($AllUsers -and (IsLocalAdministrator)) {$BaseFolder = [Environment]::GetFolderPath("CommonDesktopDirectory")}
$shortcutPath = Join-Path -Path $BaseFolder -ChildPath "$($BaseFileName).lnk"
try {
$Shortcut = $WshShell.CreateShortcut($shortcutPath)
$Shortcut.TargetPath = "https://lastpass.com/home.php"
$Shortcut.WorkingDirectory = "$InstallDir"
$Shortcut.IconLocation = "$InstallDir\lastpass.exe, 0"
$Shortcut.WindowStyle = 1
$Shortcut.Description = $BaseFileName
} catch {
$_
return
}
try {
$Shortcut.Save()
"Shortcut created successfully ($($shortcutPath))"
return
} catch {
$_
return
}
}
end {
}