我将如何在 powershell 中下载和使用图标

how would i download and use an icon in powershell

每当我在 powershell 中下载和使用 ico 文件时,它总是抛出一个错误提示“FILEPATH”不包含图标,即使它已经选择了一个 ico 文件,这就是我得到的代码

$url = "WEBSITE FAVICON URL"
Invoke-WebRequest $url -OutFile C:\Windows\temp\test.ico

$path = "path"
New-Item .\Desktop -ItemType Directory -Force

$wshshell = New-Object -ComObject WScript.Shell
$desktop = [System.Environment]::GetFolderPath('Desktop')
  $lnk = $wshshell.CreateShortcut($desktop+"\LINKNAME.lnk")
  $lnk.IconLocation = "C:\Windows\Temp\test.ico"
  $lnk.TargetPath = "MY WEBSITE URL"
  $lnk.Save()

当我 运行 填满大写字母时,快捷方式有一个空白图标,当我尝试在属性中更改它时,我被抛出这个 error,有什么办法吗?知道如何在 PowerShell 终端中下载和使用快捷方式图标吗?

假设 favicon 的 URL 确实指向一个图标文件并且您使用

下载它
$iconPath = 'C:\Windows\temp\test.ico'
# for demo I'm using the icon from whosebug.com
$url = "https://cdn.sstatic.net/Sites/Whosebug/Img/favicon.ico?v=ec617d715196"
Invoke-WebRequest -Uri $url -OutFile $iconPath

然后您想要创建一个 Internet 快捷方式 (.url),它具有与文件或路径 (.lnk) 的快捷方式不同的属性:

$shortcutName = 'Stack OverFlow'
$shortcutPath = Join-Path -Path ([environment]::GetFolderPath("Desktop")) -ChildPath ($shortcutName + '.url')
$WshShell = New-Object -ComObject WScript.Shell
$shortcut = $WshShell.CreateShortcut($shortcutPath)
# for a .url shortcut only set the TargetPath here
$shortcut.TargetPath = 'https://whosebug.com/'
$shortcut.Save()

# create a Here-String with the .url specific properties (in Name=Value format)
$urlProps = @"
Hotkey=0
IconFile=$iconPath
IconIndex=0
"@
# and add that to the newly created shortcut file
Add-Content -Path $shortcutPath -Value $urlProps

# clean up the COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($shortcut) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($WshShell) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

如果你想查看快捷方式文件中的内容,你可以这样做

Get-Content $shortcutPath

你会发现它只是一个 INI 格式的文本文件:

[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,11
[InternetShortcut]
IDList=
URL=https://whosebug.com/
Hotkey=0
IconFile=C:\Windows\temp\test.ico
IconIndex=0

其中 属性 TargetPath 现在称为 URL