You know, I’m not even sure of my visitor count these days, but last I looked this site still receives a few hundred pages views per week. Something like that, at least. I wrote something at work today that brought me over here, as it may help others. I guess I still do care enough to share, so here goes.
At work, we have a OneDrive Group Policy Object that includes a setting that doesn’t allow .lnk files (computer shortcuts), and .url files (web links) to be synced to OneDrive. There are reasons for this, but after a failed computer and a loss of my links in my browser, because yes, maybe I don’t log into my browsers to save them, I decided I needed something else.
I create a folder in OneDrive on my computer. Something like: "C:\Users\Me\Company\Desktop\Links.” Inside this directory, I create a file called _Template.html and populated it with the following HTML.
<html>
<head>
<meta http-equiv="refresh" content="0; url=##URL##">
</head>
<body>
<p>If you are not redirected automatically, follow this <a href="##URL##">##LINKTEXT##</a></p>
</body>
</html>
Every additional, non-template-based, file will originate from this template. The words with two hash marks before and after it (URL and LINKTEXT) stands in for something that will be replaced by a parameter value — an argument — passed into the function. So it’s been stated, these HTML files will open your default browser and redirect to the URL that’s included when the file is created. Best part, they sync to OneDrive. Let’s take a look at the PowerShell function.
function New-WebLink {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]$URL,
[Parameter(Mandatory)]
[Alias('Text')]
[string]$LinkText
) # end Param.
$Path = 'C:\Users\Me\Company\Desktop\Links'
$TemplateContent = Get-Content -Path "$Path\_Template.html"
$TemplateContent = $TemplateContent -replace '##URL##', $URL
$TemplateContent = $TemplateContent -replace '##LinkText##', $LinkText
Set-Content -Path "$Path\$LinkText.html" -Value $TemplateContent
} # end New-WebLink.
This function, New-WebLink, requires two parameters: URL and LinkText, of which there’s an alias, Text. Pass in arguments for those parameters at run time, and it’ll create a new file in the same location named using the LinkText argument as the name. Remember the function has to be added to the session before it can be used. You can dot-source it from inside a .ps1 file, or do what I did, and add it to your $PROFILE. Here’s an example.
New-WebLink -URL 'https://learn.microsoft.com/en-us/powershell/' -LinkText 'PowerShell Documentation'
It works perfectly and best of all, now my links are sure to be in OneDrive the next time my computer dies, because there’s always a next time.
Edit: After publishing this, I realized that I should make a change; typical. I use both Edge and Firefox. Edge with one account and Firefox with another. Therefore, I associated .html files with Edge and .htm with Firefox. And that led to this update. When using this version, include the Browser parameter and either the Edge or Firefox argument.
function New-WebLink {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]$URL,
[Parameter(Mandatory)]
[Alias('Text')]
[string]$LinkText,
[Parameter(Mandatory)]
[ValidateSet('Edge','Firefox')]
[string]$Browser
) # end Param.
$Path = 'C:\Users\Me\Company\Desktop\Links'
$TemplateContent = Get-Content -Path "$Path\_Template.html"
$TemplateContent = $TemplateContent -replace '##URL##', $URL
$TemplateContent = $TemplateContent -replace '##LinkText##', $LinkText
if ($Browser -eq 'Edge') {
Set-Content -Path "$Path\$LinkText.html" -Value $TemplateContent
} else {
Set-Content -Path "$Path\$LinkText.htm" -Value $TemplateContent
} # end if-else.
} # end New-WebLink.