Skip to content

Instantly share code, notes, and snippets.

@fatherjack
Created June 7, 2022 07:54
Show Gist options
  • Save fatherjack/69f191faf90ca840a8bcc1b2e1271d7d to your computer and use it in GitHub Desktop.
Save fatherjack/69f191faf90ca840a8bcc1b2e1271d7d to your computer and use it in GitHub Desktop.
function Format-QuotedString {
<#
.SYNOPSIS
Wraps a string in delimiters
.DESCRIPTION
Takes in a string, returns the same string wrapped in chosen delimiters. Made to fill a similar role as the TSQL QUOTENAME function.
.PARAMETER String
The string you want wrapped
.PARAMETER Delimiter
The delimiter that you want to wrap your string
.EXAMPLE
QuoteName "This is a string"
output:
'This is a string'
.EXAMPLE
QuoteName "This is a string" '/*'
output:
/*This is a string*/
.NOTES
Author Jonathan Allen
Date June 2022
#>
[Alias('Quotename')]
[cmdletbinding()]
param(
[string]$String,
[string]$Delimiter
)
$Lookup = @{
"'" = "','"
'"' = '","'
'[' = "[,]"
'(' = '(,)'
'{' = '{,}'
'<' = '<,>'
'-' = '-,-'
'/*' = '/*,*/'
'<#' = '<#,#>'
}
if ($Delimiter -eq '') { $Delimiter = "'" }
$Delimiter1, $Delimiter2 = $Lookup[$Delimiter] -split ','
# return the wrapped string
return "$Delimiter1$String$Delimiter2"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment