Skip to content

Instantly share code, notes, and snippets.

@stknohg
Created January 16, 2017 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/d34b50b5bba4f0e0b771d56f00fec7d4 to your computer and use it in GitHub Desktop.
Save stknohg/d34b50b5bba4f0e0b771d56f00fec7d4 to your computer and use it in GitHub Desktop.
スタートメニューにexeファイルのショートカットファイルを置くスクリプト
#
# スタートメニューにexeファイルのショートカットファイルを置くスクリプト
#
# 検索対象ディレクトリ
$SearchDir = "C:\Program Files2"
# ショートカットファイル置き場
$OutputPath = Join-Path ([Environment]::GetFolderPath([Environment+SpecialFolder]::StartMenu)) "MyShortcuts"
#
# ショートカットを作る雑なファンクション
#
function New-Shortcut([string]$Path, [string]$Name, [string]$OutputPath ) {
if (-not (Test-Path $OutPutPath -PathType Container)) {
mkdir $OutPutPath
}
try {
$lnkName = Join-Path $OutputPath ($Name + ".lnk")
$wsh = New-Object -ComObject "WScript.Shell"
$link = $wsh.CreateShortcut($lnkName)
$link.TargetPath = $Path
$link.WorkingDirectory = ([System.IO.FileInfo]$Path).Directory
$link.Save()
} finally {
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($wsh)
}
}
# 指定されたディレクトリ配下で、ディレクトリ名=プログラム名 となっているexeファイルを取得
$Files = Get-ChildItem -LiteralPath $SearchDir -Recurse -File -Include "*.exe" | Where-Object { $_.Name -eq ($_.Directory.Name + ".exe") }
# ショートカット作成
$Files | ForEach-Object {
New-Shortcut -Path $_.FullName -Name ([IO.Path]::GetFileNameWithoutExtension($_)) -OutPutPath $OutputPath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment