Tobe
Tobe

Reputation: 93

Powershell, URL shortcut

I've got a problem and i need your help. I'm trying to do a shortcut from the active URL. I tried a few things and got to this.

Param([switch]$Full, [switch]$Location, [switch]$Content)
$urls = (New-Object -ComObject Shell.Application).Windows() |
Where-Object {$_.LocationUrl -match "(^https?://.+)|(^ftp://)"} |
Where-Object {$_.LocationUrl}
if($Full)
{
    $urls
}
elseif($Location)
{
    $urls | select Location*
}
elseif($Content)
{
    $urls | ForEach-Object {
        $ie.LocationName;
        $ie.LocationUrl;
        $_.Document.body.innerText
    }
}
else
{
    $urls | ForEach-Object {$_.LocationUrl}
}
$Shortcut = $WshShell.CreateShortcut("E:\Powershell\Ziel\short.lnk")
$Shortcut.TargetPath = "$urls"
$Shortcut.Save()

But i get an shortcut which makes no sense. What do i do wrong? I'm happy about any suggestion.

I tried now doing it like this:

Param([switch]$Full, [switch]$Location, [switch]$Content)
$urls = (New-Object -ComObject Shell.Application).Windows() |
Where-Object {$_.LocationUrl -match "(^https?://.+)|(^ftp://)"} |
Where-Object {$_.LocationUrl}
if($Full)
{
    $urls
}
elseif($Location)
{
    $urls | select Location*
}
elseif($Content)
{
    $urls | ForEach-Object {
        $ie.LocationName;
        $ie.LocationUrl;
        $_.Document.body.innerText
    }
}
else
{
    $urls | ForEach-Object {$_.LocationUrl}
}

$url = $urls | ForEach-Object {$_.LocationUrl} | select -First 1
$Shortcut = $WshShell.CreateShortcut("E:\Powershell\Ziel\short.lnk")
$Shortcut.TargetPath = $url
$Shortcut.Save()

But no it tells me that "$Shortcut = $WshShell.CreateShortcut("E:\Powershell\Ziel\short.lnk")" has the value NULL. I mean, how is that even possible. I don't get it. Please help.

Upvotes: 0

Views: 1570

Answers (1)

Avshalom
Avshalom

Reputation: 8899

This one is wrong... (this is not a valid url, it just an array of objects)

$Shortcut.TargetPath = "$urls"

You need to select one of the url's first, for example:

$url = $urls | ForEach-Object {$_.LocationUrl} | select -First 1

Then:

$Shortcut = $WshShell.CreateShortcut("E:\Powershell\Ziel\short.lnk")
$Shortcut.TargetPath = $url
$Shortcut.Save()

if you want to create a url for each of the URL's Array, then you can use foreach, like this:

foreach ($url in $URLs) 
{
    $UrlName = $url.LocationName.Substring(0,8)
    $Link = $url.LocationUrl
    $Shortcut = $WshShell.CreateShortcut("E:\Powershell\Ziel\$UrlName.lnk")
    $Shortcut.TargetPath = $Link
    $Shortcut.Save()
}

Upvotes: 1

Related Questions