Get String as a PowerShell Function Argument Without Quotes Necessary

Pavol Kutaj
1 min readFeb 3, 2021

usecase

The concern is documenting the Powershell’s pattern to accept an argument as [string] with multiple words without quotes necessary.

1. search-google

Function search-google {
$query = 'https://www.google.com/search?q='
$args | % { $query = $query + "$_+" }
$url = $query.Substring(0, $query.Length - 1)
start "$url"
}
  • this works

2. convert to filename

  • the aim here is to pass a string say
90.1 Sparse is Better than Beautiful
  • and get copied to the clipboard
90.1-Sparse-is-Better-than-Beautiful.md
  • this was my old way — with quotes necessary around the argument
function convertTo-filename ($docTitle) {
$filename = ("$docTitle" -replace "\s", "-") -replace ".+", "$&.md"
$filename | clip
Write-Host "$filename clipped" -ForegroundColor Darkcyan
}
▶ convertTo-filename "90.1 Sparse is Better than Beautiful"
90.1-Sparse-is-Better-than-Beautiful.md clipped
  • new
function convertTo-filename {
$docTitle = ""
$args | % {$docTitle += "$_ "}
$docTitle = $docTitle.substring(0, $docTitle.Length - 1)
$filename = ("$docTitle" -replace "\s", "-") -replace ".+", "$&.md"
$filename | clip
Write-Host "$filename clipped" -ForegroundColor Darkcyan
}
▶ convertTo-filename 90.1 Sparse is Better than Beautiful
90.1-Sparse-is-Better-than-Beautiful.md clipped

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com