0

I'm trying to create a PowerShell script to create website shortcuts on the desktop from a text file located on the desktop. Text text file has the URL and website names.

STerliakov
  • 4,983
  • 3
  • 15
  • 37
  • 6
    Hey, welcome to SO! It looks like you clicked the Submit button too fast and forgot to add the code sample that you are having trouble with. Did you know, you can improve your post by clicking the Edit link below it. You may also have a look at the [help] section, specifically [ask] a good question. Have a nice day! – zett42 Mar 23 '22 at 20:20
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 24 '22 at 08:50

1 Answers1

1

I made a script that is a bit flexible. The following script shows some examples of how to use it. The last example shows how it works with a text file with URLs and names in the same line. That would already work in most cases to get at least some links generated. The script may be customized to fit your text file, but you must show examples.

Function Remove-InvalidFileNameChars {
    param(
    [Parameter(Mandatory=$true,
    Position=0,
    ValueFromPipeline=$true,
    ValueFromPipelineByPropertyName=$true)]
    [String]$Name
    )
    
    $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
    $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
    return ($Name -replace $re,"_")
} # Source: https://stackoverflow.com/questions/23066783/how-to-strip-illegal-characters-before-trying-to-save-FilePaths

function New-Link {
    param(
    [Parameter(Mandatory=$true,
    Position=0,
    ValueFromPipeline=$true,
    ValueFromPipelineByPropertyName=$true)]
    [Alias("Address","LINK")]
    [String]$URL,
    [Parameter(Mandatory=$true,
    Position=1,
    ValueFromPipeline=$true,
    ValueFromPipelineByPropertyName=$true)]
    [ValidateSet("URL", "HTML")]
    [String]$Type = "URL",
    [Parameter(Mandatory=$false,
    Position=2,
    ValueFromPipeline=$true,
    ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNullOrEmpty()]
    [ValidateScript({if ([System.IO.FileInfo]$_) {$true} else{throw "Exception: The parent directory was not found."}})]
    [Alias("Name","Path")]
    [System.IO.FileInfo]$FilePath
    )
    
    if (-not $FilePath) { $FilePath = (Remove-InvalidFileNameChars $URL) + "." + $Type }
    if ($FilePath -notlike "*.$Type") {$FilePath = [string]$FilePath + "." + $Type}
    $FilePath = ([string]$FilePath).trim() # Remove leading or trailing spaces
    
    switch ( $Type ) {
        "URL" {
            # Windows only - it's windows default format for URLs
            "[InternetShortcut]`r`nURL=$URL`r`n" | Out-File $FilePath
        }
        
        "HTML" {
            # Compatible to multiple OS/Browsers but not with smartphones when not accessed via browser and webserver
            # - Idea from https://www.ctrl.blog/entry/internet-shortcut-files.html - More ideas for alternative types are there like 
            # - You might see the local file within browsers addressbar as long as the target site does not answer
            "<!doctype html>",
            "<script>",
            "window.location.replace('$URL')",
            "</script>",
            "" | Out-File $FilePath
        }
    }
}

#Examples for Web
New-Link -URL stackoverflow.com -Type url
New-Link -URL https://stackoverflow.com -Type url
New-Link -URL stackoverflow.com -Type HTML
New-Link -URL https://stackoverflow.com -Type HTML
New-Link -URL https://stackoverflow.com/questions/71593413/powershell-script-to-create-website-shortcuts-from-text-file -Type HTML
New-Link -URL stackoverflow.com -Type HTML -FilePath "Where Developers Learn, Share, & Build ..."


#Other Examples
New-Link -URL ldap://yourDomainController -Type URL
New-Link -URL tel:00123456789 -Type URL -Name CallMe
New-Link -URL mailto:noreply@DSmoothPanda -Type URL -Name MailMe.url
New-Link -URL FTP:yourWebServer -Type URL -Name UploadFilesToMe


## Example of generating URL files for links with names in the same line
# ExampleContent for "links.txt"
"
https://website.com/what?you%20want page with more data
another link that I will make a file for https://www.google.com/search?q=StackOverflow
this line will not generate a link as it doesn't have something that can be detected
the next line will generate only the first link, and everything else will be the name
my holiday pictures are http://myholidaypicturepage.com here http://onedrive.com
next-line will generate a filename from URL
https://stackoverflow.com/questions/71593413/powershell-script-to-create-website-shortcuts-from-text-file
    " | Out-File .\links.txt
    
    # Generation of URL files
    foreach ($line in (Get-Content .\links.txt | where {$_ -match "(https?:|mailto:|ftp:|tel:)"})) {
        $URL = @($line -split " " | where {$_ -match "^https?:"})[0]
        $FilePath = ($line -replace [Regex]::Escape($url),"").trim()
        
        "`r`n`tGenerating Link with following parameters"
        " Filename: $FilePath"
        " URL: $URL"
        if ($FilePath) {
                                  
            New-Link -URL $URL -Type URL -FilePath $(Remove-InvalidFileNameChars $FilePath)
            } else {
            New-Link -URL $URL -Type URL
        }
    }

The following test files will be generated in the current folder to better understand what my script does: another link that I will make a file for.URL

CallMe.URL
https___stackoverflow.com.HTML
https___stackoverflow.com.url
https___stackoverflow.com_questions_71593413_powershell-script-to-create-website-shortcuts-from-text-file.HTML
https___stackoverflow.com_questions_71593413_powershell-script-to-create-website-shortcuts-from-text-file.URL
ldap___yourDomainController.URL
links.txt
MailMe.url
my holiday pictures are  here http___onedrive.com.URL
page with more data.URL
stackoverflow.com.HTML
stackoverflow.com.url
UploadFilesToMe.URL
Where Developers Learn, Share, & Build ....HTML
An-dir
  • 465
  • 2
  • 13