<p>How do I convert function input parameters to the right type?</p>

<p>I want to return a string that has part of the URL passed into it removed.</p>

<p><strong>This works, but it uses a hard-coded string:</strong></p>

<pre><code>function CleanUrl($input)
{
    $x = "http://google.com".Replace("http://", "")
    return $x
}

$SiteName = CleanUrl($HostHeader)
echo $SiteName
</code></pre>

<p><strong>This fails:</strong></p>

<pre><code>function CleanUrl($input)
{
    $x = $input.Replace("http://", "")
    return $x
}

Method invocation failed because [System.Array+SZArrayEnumerator] doesn't contain a method named 'Replace'.
At M:\PowerShell\test.ps1:13 char:21
+     $x = $input.Replace( &lt;&lt;&lt;&lt; "http://", "")
</code></pre>

[{'answer_id': 15068, 'author': 'ESV', 'author_id': 150, 'author_profile': 'https://Stackoverflow.com/users/150', 'pm_score': 3, 'selected': False, 'text': '<pre><code>function CleanUrl([string] $url)\n{\n    return $url.Replace("http://", "")\n}\n</code></pre>\n'}, {'answer_id': 15094, 'author': 'EBGreen', 'author_id': 1358, 'author_profile': 'https://Stackoverflow.com/users/1358', 'pm_score': -1, 'selected': False, 'text': '<p>This worked for me:</p>\n\n<pre><code>function CleanUrl($input)\n{\n    return $input.Replace("http://", "")\n}\n</code></pre>\n'}, {'answer_id': 15136, 'author': 'Steven Murawski', 'author_id': 1233, 'author_profile': 'https://Stackoverflow.com/users/1233', 'pm_score': 5, 'selected': True, 'text': '<p>The concept here is correct.</p>\n\n<p>The problem is with the variable name you have chosen. $input is a reserved variable used by PowerShell to represent an array of pipeline input. If you change your variable name, you should not have any problem.</p>\n\n<p>PowerShell does have <a href="https://technet.microsoft.com/en-us/library/hh847759.aspx" rel="nofollow noreferrer">a replace operator</a>, so you could make your function into</p>\n\n<pre><code>function CleanUrl($url)\n{\n    return $url -replace \'http://\'\n}\n</code></pre>\n'}, {'answer_id': 68811, 'author': 'Jaykul', 'author_id': 8718, 'author_profile': 'https://Stackoverflow.com/users/8718', 'pm_score': 4, 'selected': False, 'text': '<p>Steve\'s answer works. The problem with your attempt to reproduce ESV\'s script is that you\'re using <code>$input</code>, which is a reserved variable (it automatically collects multiple piped input into a single variable).</p>\n\n<p>You should, however, use .Replace() unless you need the extra feature(s) of -replace (it handles regular expressions, etc).</p>\n\n<pre><code>function CleanUrl([string]$url)\n{\n    $url.Replace("http://","")\n}\n</code></pre>\n\n<p>That will work, but so would:</p>\n\n<pre><code>function CleanUrl([string]$url)\n{\n    $url -replace "http://",""\n}\n</code></pre>\n\n<p>Also, when you invoke a PowerShell function, don\'t use parenthesis:</p>\n\n<pre><code>$HostHeader = "http://google.com"\n$SiteName = CleanUrl $HostHeader\nWrite-Host $SiteName\n</code></pre>\n\n<p>Hope that helps.  By the way, to demonstrate $input:</p>\n\n<pre><code>function CleanUrls\n{\n    $input -replace "http://",""\n}\n\n# Notice these are arrays ...\n$HostHeaders = @("http://google.com","http://stackoverflow.com")\n$SiteNames = $HostHeader | CleanUrls\nWrite-Output $SiteNames\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/15062', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/636/']