Skip to content

Instantly share code, notes, and snippets.

@pandieme
Created April 3, 2022 23:49
Show Gist options
  • Save pandieme/636d9e277c539838f77e29eb3e8a5842 to your computer and use it in GitHub Desktop.
Save pandieme/636d9e277c539838f77e29eb3e8a5842 to your computer and use it in GitHub Desktop.
function Format-Query(
[Parameter(Mandatory)]
[hashtable]
$Table
) {
<#
.SYNOPSIS
Format URI query
.DESCRIPTION
Take a hashtable and format as a URI query string
#>
$Query = @()
$Table.GetEnumerator() | ForEach-Object {
$Query += ($_.Key + "=" + $_.Value)
}
return $Query | Join-String -Property $_ -Separator "&" -OutputPrefix "?"
}
@pandieme
Copy link
Author

pandieme commented Apr 3, 2022

Example:

Format-Query -Table @{
    Name = "Richard"
    Type = "Person"
}

Output:

?Name=Richard&Type=Person

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment