We're currently in the process of remapping all network drives to a new fileserver and with this, took it upon ourselves to map all of the fileshares to a uniform system (ex. everyone's public drive is Z:). With this, several people have shortcuts on their desktop. Is there a way in powershell we'd be able to change over the filepath of .lnk's from \oldfs to \newfs.
` $folder = "C:\Users$env:USERNAME\OneDrive\Desktop"
$from = "[A-Z]:"
$to = \\newfs
Get-ChildItem -Path $folder -Filter *.lnk -Recurse | ForEach-Object {
$WshShell = New-Object -ComObject WScript.Shell
$ShortcutFile = "$($_.FullName)"
$Shortcut = $WshShell.CreateShortcut($ShortcutFile)
if ($Drive.RemotePath -like "*oldfs*") {
$Shortcut.TargetPath = $Shortcut.TargetPath -replace $from, $to
$Shortcut.Save()
}
}
` This is what we've come up with so far. Any help would be appreciated :)
I've ran through a couple different scripts.
function Resolve-ShortcutTarget {
param (
[string]$shortcutPath
)
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath)
$targetPath = $shortcut.TargetPath
if ($targetPath -match '^([a-z]):') {
$driveLetter = $matches[1]
$networkDrive = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DeviceID -eq "$($driveLetter):" }
if ($networkDrive) {
$networkPath = $networkDrive.ProviderName
$targetPath = $targetPath -replace "^$($driveLetter):", $networkPath
}
}
return $targetPath
}
$shell = New-Object -ComObject Shell.Application
$desktopPath = Join-Path $env:SystemDrive "Users\$env:USERNAME\Desktop"
$desktopItems = $shell.Namespace($desktopPath).Items()
$desktopInfoList = foreach ($item in $desktopItems) {
if ($item.IsFileSystem) {
$targetPath = $item.Path
if ($item.Type -eq 'Shortcut') {
try {
$targetPath = Resolve-ShortcutTarget -shortcutPath $targetPath
} catch {
Write-Warning "Failed to resolve target path for shortcut: $targetPath"
$targetPath = $null
}
}
$computerName = $env:COMPUTERNAME
try {
$owner = (Get-Acl -LiteralPath $targetPath -ErrorAction Stop).Owner
} catch {
Write-Warning "Failed to get ACL for: $targetPath"
$owner = $null
}
$directoryName = $null
if ($targetPath) {
try {
$directoryName = (Get-Item $targetPath -ErrorAction Stop).DirectoryName
} catch {
Write-Warning "Failed to get directory information for: $targetPath"
}
}
[PSCustomObject]@{
"Name" = $item.Name
"DirectoryName" = $directoryName
"Directory" = $targetPath
"Computer" = $computerName
"Owner" = $owner
"Type" = $item.Type
"Target" = $targetPath
}
}
}
# Export the information to a CSV file
$desktopInfoList | Export-Csv -Path "C:\Users\$env:USERNAME\Desktop\desktop_items.csv" - NoTypeInformation
`
This did work in terms of locating & listing all of the shortcuts on one's desktop. I did run into null value issues though.