When I run the following powershell script it outputs the results twice when there is a comma it separates the strings as two entries. How can I make it treat it as one string instead of two?

Function Get-Weather {
                Mandatory = $true,
                HelpMessage = 'Enter name of the City to get weather report',
                ValueFromPipeline = $true,
                Position = 0
            [string[]] $City,
            [switch] $Tomorrow,
            [switch] $DayAfterTomorrow

        Foreach($Item in $City){
            try {

                # Check Operating System Version
                If((Get-WmiObject win32_operatingsystem).caption -like "*Windows 10*") {
                    $Weather = $(Invoke-WebRequest "http://wttr.in/$City" -UserAgent curl -UseBasicParsing).content -split "`n"
                else {
                    $Weather = (Invoke-WebRequest "http://wttr.in/$City" -UseBasicParsing).ParsedHtml.body.outerText  -split "`n"

                    If($Tomorrow){ $Weather[17..26] }
                    If($DayAfterTomorrow){ $Weather[27..36] }
            catch {


Get-Weather Shrewsbury,MA?n1
  • 2
    Commas are array separators in PowerShell. You should either escape them or encase the parameter in double quotes and then write some logic to split your string that contains commas. E.g. Your cmdlet call should be: Get-Weather "Shrewsbury,MA?n1" - But make sure you have some function to, perhaps, break the parameter into an array (delimited by the comma) and then use the array index to get whichever part of the parameter the rest of your script needs. – Kinnectus Mar 29 at 14:52
  • Perfect thanks. Researching this as a newbie to powershell gave me no straight answers. Very helpful! – Mark Deven Mar 29 at 16:02

Also remember that when you use single quotes "single-quotation marks (a single-quoted string), the string is passed to the command exactly as you type it. No substitution is performed." Versus when you use double quotes "double quotation marks (a double-quoted string), variable names that are preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing."

This is from: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6 and is for PowerShell 6, but the same rules apply to PowerShell 1 through 6.

So unless you need to use a variable inside the string, the single quotes will force PowerShell to use the string exactly how you have it written.

  • Thank you. I never knew that. – Mark Deven Apr 13 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.