whenever i download and use an ico file in powershell it always throws me an error saying "FILEPATH" contains no icons even though it has selected an ico file this is the code i got
$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()
and when i run this with the caps filled in then the shortcut has a blank icon and when i try and change it in properties i get thrown this error, is there any way anybody knows how to download and use a icon for a shortcut in a PowerShell terminal?
Suppose the URL for the favicon is indeed pointing to an icon file and you download it using
$iconPath = 'C:\Windows\temp\test.ico'
# for demo I'm using the icon from stackoverflow.com
$url = "https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico?v=ec617d715196"
Invoke-WebRequest -Uri $url -OutFile $iconPath
You then want to create an Internet shortcut (.url) which has different properties than a shortcut to a file or path (.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://stackoverflow.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()
If you want to see what is in the shortcut file, you can do
Get-Content $shortcutPath
where you will find it is just a text file in INI format:
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,11
[InternetShortcut]
IDList=
URL=https://stackoverflow.com/
Hotkey=0
IconFile=C:\Windows\temp\test.ico
IconIndex=0
where property TargetPath
is now called URL