PowerShell创建带自定义图标的桌面.url快捷方式报错及图标失效问题
问题:PowerShell创建URL快捷方式时图标失效并报错
需求是编写PowerShell脚本批量部署网页桌面快捷方式并自定义图标,但运行脚本时出现报错,且图标无法正常应用。
原脚本
# region Functions Push-Location $PSScriptRoot # Initial values $Icon_start_path = ".\Icon.ico" $Icon_end_folder = "C:\Program Files\Icons" $Icon_end_path = "C:\Program Files\Icons\Icon.ico" $Shortcut_end_path = "C:\Users\Public\Desktop\website.url" $Target_path = "https://website.co.uk/" $Icon_filename = "Icon.ico" # If previous shortcut is there, delete it Get-ChildItem $Shortcut_end_path -ErrorAction SilentlyContinue | Remove-Item -ErrorAction SilentlyContinue # Check if icon file has been copied to $Icon_end_folder if (Test-Path $Icon_end_folder) { Write-Host "Icons folder already exists." } else { #Create directory if it doesn't exists New-item -Path $Icon_end_folder -ItemType Directory Write-Host "Icons folder created." } # Copy the icon file over Copy-Item -Path $Icon_start_path -Destination $Icon_end_folder # Create the shortcut $shell = New-Object -ComObject WScript.Shell $shortcut = $Shell.CreateShortcut($Shortcut_end_path) $shortcut.TargetPath = $Target_path $Shortcut.IconLocation = $Icon_end_path $shortcut.Save()
报错信息
The shortcut pathname must end with .lnk or .url. At line:2 char:1 + $shortcut = $Shell.CreateShortcut($Shortcut_end_path) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException The property 'TargetPath' cannot be found on this object. Verify that the property exists and can be set. At line:3 char:1 + $shortcut.TargetPath = $Target_path + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound The property 'IconLocation' cannot be found on this object. Verify that the property exists and can be set. At line:4 char:1 + $Shortcut.IconLocation = "C:\Program Files\NHS_Icons\CSMS.ico" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound You cannot call a method on a null-valued expression. At line:5 char:1 + $shortcut.Save() + ~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
问题原因
WScript.Shell的CreateShortcut方法对.url快捷方式的支持有限:
- 虽然报错提示路径需以
.lnk或.url结尾,但实际调用后返回的对象并不支持TargetPath和IconLocation这些属性(这些是.lnk快捷方式专属属性) .url本质是INI格式的纯文本文件,需要直接写入对应字段来设置目标地址和图标
修复后的脚本
Push-Location $PSScriptRoot # 初始化参数 $Icon_start_path = ".\Icon.ico" $Icon_end_folder = "C:\Program Files\Icons" $Icon_end_path = Join-Path -Path $Icon_end_folder -ChildPath "Icon.ico" $Shortcut_end_path = "C:\Users\Public\Desktop\website.url" $Target_path = "https://website.co.uk/" # 删除旧快捷方式 if (Test-Path -Path $Shortcut_end_path) { Remove-Item -Path $Shortcut_end_path -Force Write-Host "已删除旧的快捷方式" } # 创建图标目录(如果不存在) if (-not (Test-Path -Path $Icon_end_folder)) { New-Item -Path $Icon_end_folder -ItemType Directory | Out-Null Write-Host "已创建图标目录" } else { Write-Host "图标目录已存在" } # 复制图标文件(覆盖已存在的文件) Copy-Item -Path $Icon_start_path -Destination $Icon_end_path -Force Write-Host "图标文件已复制到位" # 构建URL快捷方式的内容(INI格式) $shortcutContent = @" [InternetShortcut] URL=$Target_path IconFile=$Icon_end_path IconIndex=0 "@ # 写入快捷方式文件 $shortcutContent | Out-File -Path $Shortcut_end_path -Encoding ASCII Write-Host "URL快捷方式已创建,图标已设置" Pop-Location
修复说明
- 直接操作.url文件:放弃
WScript.Shell的COM对象,直接生成符合INI格式的.url文件,这是设置URL快捷方式图标的正确方式 - 核心字段说明:
URL:指定网页目标地址IconFile:指定图标文件的绝对路径IconIndex:图标索引,通常设为0(若图标文件包含多个图标,可指定对应索引)
- 操作优化:
- 用
Join-Path拼接路径,避免手动拼接出现格式错误 - 添加
-Force参数确保覆盖已存在的文件 - 用
Out-Null抑制不必要的输出,让脚本日志更简洁
- 用
内容的提问来源于stack exchange,提问作者InvertedEight
相关产品推荐
相关产品推荐

