Skip to content

Instantly share code, notes, and snippets.

@griffeth-barker
Last active August 2, 2025 15:20
Show Gist options
  • Save griffeth-barker/475c23cd987067301140c39d28d0cac5 to your computer and use it in GitHub Desktop.
Save griffeth-barker/475c23cd987067301140c39d28d0cac5 to your computer and use it in GitHub Desktop.
Generate a shortened Uri using TinyURL
function New-ShortenedUri {
<#
.SYNOPSIS
Genereates a shortened Uri.
.DESCRIPTION
Genereates a shorted Uri based on the provided Uri using the TinyUrl service.
.PARAMETER Uri
A mandatory Uri to be shortened.
.PARAMETER CopyToClipboard
An optional switch that copies the shortened Uri to the clipboard if specified.
.EXAMPLE
PS:\> New-ShortenedUri -Uri 'https://domain.tld' -CopyToClipBoard
Generates a shortened Uri and copies it to the clipboard.
.INPUTS
[System.Uri]
.OUTPUTS
[System.String]
.NOTES
This function requires an internet connection to access the TinyUrl API endpoint.
This function requires the Microsoft.PowerShell.Management and Microsoft.PowerShell.Utility modules.
#>
#Requires -Modules Microsoft.PowerShell.Management
#Requires -Modules Microsoft.PowerShell.Utility
[CmdletBinding()]
[Alias("nsu")]
param(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ValueFromPipeline = $true)]
[System.Uri]$Uri,
[Parameter(Mandatory = $false)]
[switch]$CopyToClipboard
)
begin {}
process {
$output = [System.Uri]::new("$(Invoke-RestMethod -Method GET -Uri "http://tinyurl.com/api-create.php?url=$uri")").AbsoluteUri
if ( $PSBoundParameters.ContainsKey('CopyToClipboard') ) {
$output | Set-Clipboard
}
$output
}
end {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment