Powershell更改所有文件夹和子文件夹中的快捷方式的路径

时间:2020-08-07 07:02:51

标签: powershell

有人可以帮助我解决我的问题吗? 我想更改所有文件夹和子文件夹中的快捷方式的路径,因为我们已经更改了服务器。这就是为什么我需要代码循环遍历所有文件夹和子文件夹的原因。

旧路径定义为oldPrefix,路径定义为newPrefix,我刚刚在这里放置了占位符。 该代码当前仅在桌面上搜索快捷方式,我需要它在桌面上的所有内容之间进行搜索(所以可能会有某种for循环?)。

$oldPrefix = "OLD SERVER"
$newPrefix = "NEW SERVER"
$searchPath = "$($env:USERPROFILE)\Desktop"

$shell = new-object -com wscript.shell
write-host "Updating shortcut target" -foregroundcolor red -backgroundcolor black

dir $searchPath -filter *.lnk -recurse | foreach {
$lnk = $shell.createShortcut( $_.fullname )
$oldPath= $lnk.targetPath
$lnkRegex = "^" + [regex]::escape( $oldPrefix )

if ( $oldPath -match $lnkRegex ) {
$newPath = $oldPath -replace $lnkRegex, $newPrefix

write-host "Found: " + $_.fullname -foregroundcolor yellow -backgroundcolor black
write-host " Replace: " + $oldPath
write-host " With: " + $newPath
$lnk.targetPath = $newPath
$lnk.Save()
}
}

谢谢。

1 个答案:

答案 0 :(得分:0)

如果将目录行更改为此,它将是递归的,并且也会在子目录中获得所有* .lnk文件。

Get-ChildItem -Path $searchPath -Filter *.lnk -Recurse | foreach {
相关问题