I have a situation where all the paths in the shortcut files that are located in the %AppData%\Microsoft\Windows\Start Menu\Programs
folder and subfolders all point to an incorrect drive letter. This includes the Target: value, Start In: value and all the paths to the icon files as well. I'd like to change them all from X:\
to C:\
There are a couple that are correctly pointing to C:\
but there's only a handful of them.
Here is the code I was working with. I am able to change the TargetPath
but not the WorkingDirectory
value. I've tried removing the comment on line 20 but that creates an error about $null
-valued expression. I've also tried duplicating the bit for TargetPath
to WorkingDirectory
but it does not change:
$folder = "C:\Temp\Shortcuts"
[string]$from = "X:\"
[string]$to = "C:\"
$list = Get-ChildItem -Path $folder -Recurse | Where-Object { $_.Attributes -ne "Directory"} | select -ExpandProperty FullName
$obj = New-Object -ComObject WScript.Shell
ForEach($lnk in $list)
{
$obj = New-Object -ComObject WScript.Shell
$link = $obj.CreateShortcut($lnk)
[string]$path = $link.TargetPath
[string]$path = [string]$path.Replace($from.tostring(),$to.ToString())
# [string]$path = $link.WorkingDirectory
# [string]$path = [string]$path.Replace($from.tostring(),$to.ToString())
#If you need workingdirectory change please uncomment the below line.
#$link.WorkingDirectory = [string]$WorkingDirectory.Replace($from.tostring(),$to.ToString())
$link.TargetPath = [string]$path
$link.Save()
}