PowerShell - Clickable Links

Creating clickable links in code 🎉

The Issue

Most of the time, when using scripting in Powershell, you have your file path export for example to say "$env:Temp\config1.yaml" and you’ll have that dance with the terminal of trying to copy the text, it sometimes copying and sometimes not… Maybe its just me. So I though this MUST be possible right. Its just a time and resource query.

So I took to the interwebs and started searching as you do, Checking out Stack Overview, GitHub, and some All knowing AI (ChatGPT). During my investigation I found some interesting links from StackOverflow which made it look like it might be possible:

The Solution

The second Stack Overflow Post, Found the answer that I needed using escape characters in PowerShell. If you want to read more about the `e character, You can check out the Microsoft Docs page Here

Pwsh Requirement - PowerShell 7.x

1
2
3
$url = "https://blog.builtwithcaffeine.cloud"
$name = "BuiltWithCaffeine :: Blog"
Write-Output "`e]8;;$url`e\$name`e]8;;`e\"

You can use both: Write-Output Write-Host

If you wanted to add some colour to your links:

1
2
3
$url = "https://blog.builtwithcaffeine.cloud"
$name = "BuiltWithCaffeine :: Blog"
Write-Host -ForegroundColor 'Magenta' "`e]8;;$url`e\$name`e]8;;`e\"

Write-Host Examples

Magenta

White

Yellow