tomoko523
10/1/2015 - 8:02 AM

ChangeLink.ps1

# フォルダ内のショートカットを検索し、ショートカットの内容を変更する。
clear-host

# 値を設定
$folder = "…" # 検索対象フォルダ
write-host "対象フォルダ:" $folder
$search = "…" # 置換前
$replace = "…" #置換後
$filter = "…" + "*" #前方一致で検索

"ショートカットを検索中。しばらくお待ちください ..."
$shortcuts = get-childitem -path $folder -recurse -include *.lnk
$count = $shortcuts.length
if ($count -eq 0) {
"ショートカットがみつかりませんでした。", "終了します。"
exit 9
}
$shell = new-object -comobject WScript.Shell
write-host $count "件のショートカットがみつかりました。"

$shortcuts | foreach-object {
	echo $_.fullname
	$wsc = $shell.CreateShortcut($_.fullname)
	
	# ショートカット一覧が欲しいだけの場合はここまで
	# echo $wsc.TargetPath

	# パス変換
	if ($wsc.TargetPath -ilike $filter) {

		echo "変更前" $wsc.TargetPath
		$wsc.TargetPath = $wsc.TargetPath.Replace($search, $replace)
		$wsc.Save()
		echo "変更後" $wsc.TargetPath

	}
		#elseif ($wsc.TargetPath -ilike "…*"){
		#   
		#    # 他に何か置換したいとき   
		#
		#    } 
	echo "未変更" $wsc.TargetPath
}