Skip to content

Instantly share code, notes, and snippets.

@seayxu
Created May 6, 2022 10:51
Show Gist options
  • Save seayxu/68ff7f1e5c7f2c692b6850d1ca211cdc to your computer and use it in GitHub Desktop.
Save seayxu/68ff7f1e5c7f2c692b6850d1ca211cdc to your computer and use it in GitHub Desktop.
create startup by sendto create app shortcut or copy app shortcut
pwsh -ExecutionPolicy ByPass -NoProfile "%~dp0Startup.ps1" %*
# create startup by sendto create app shortcut or copy app shortcut
function Main {
param (
[string[]] $apps
)
if ($apps.Count -lt 1) {
PrintHelp;
return;
}
for ($i = 0; $i -lt $apps.Count; $i++) {
CopyToStartup($apps[$i].Trim());
}
}
function PrintHelp {
Write-Output "Create Startup from SendTo";
Write-Output " 1:Add this to SendTo";
Write-Output " 2:Remove this from SendTo";
Write-Output "Press any key to exit.";
$key = Read-Host "input action";
$st = [System.Environment]::GetFolderPath("SendTo");
switch ($key) {
"1" {
$root = Get-Location;
$app = [IO.Path]::Combine($root, "Startup.cmd");
$ico = [IO.Path]::Combine($root, "startup.ico");
CreateShortcut -app $app -dir $st -icon $ico;
}
"2" {
$dest = [IO.Path]::Combine($st, "Startup.lnk");
if (Test-Path $dest) {
Remove-Item -Path $dest;
Write-Output "Remove startup:"+$dest;
}
}
}
}
function CopyToStartup {
param (
[string] $app
)
if ([string]::IsNullOrWhiteSpace($app) -eq 1) {
return;
}
$ext = [IO.Path]::GetExtension($app);
$startup = [System.Environment]::GetFolderPath("CommonStartup");
switch ($ext) {
".lnk" {
Write-Output "Creating startup:"+$app;
$dest = [IO.Path]::Combine($startup, [IO.Path]::GetFileName($app));
Copy-Item -Path $app -Destination $dest -Force
Write-Output "Created startup:"+$dest;
}
Default {
CreateShortcut -app $app -dir $startup;
}
}
}
function CreateShortcut() {
param (
[string] $app,
[string] $dir,
[string] $icon
)
$dest = [IO.Path]::Combine($dir, [IO.Path]::GetFileNameWithoutExtension($app) + ".lnk");
Write-Output "Creating Shortcut:"+$app;
Write-Output "Shortcut dest:"+$dest;
$shell = New-Object -ComObject WScript.Shell;
$shortcut = $shell.CreateShortcut($dest);
$shortcut.TargetPath = $app;
if ($icon -ne $null -and (Test-Path $icon)) {
$shortcut.IconLocation = $icon
}
$shortcut.Save();
Write-Output "Created Shortcut:"+$dest;
}
Main($args);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment