title: Powershell: Defining the Verbose switch in a function
body: <p>Referring <a href="http://blogs.msdn.com/b/powershell/archive/2007/02/25/supporting-whatif-confirm-verbose-in-scripts.aspx" rel="noreferrer">this</a> link, I am trying to introduce verbose mode in my script.</p>
  
  <p>When I have a function defined like this - </p>
  
  <pre><code>function TestVerbose
  {
      param(
          [switch]$verbose,
          [Parameter(Mandatory = $True)] 
          $p1
      )
  
      if($verbose)
      {
       Write-Verbose "Verbose Mode"
      }
  }
  
  Get-Help TestVerbose
  </code></pre>
  
  <p>I get the following error -</p>
  
  <blockquote>
    <p>Get-Help : A parameter with the name 'Verbose' was defined multiple
    times for the command. At line:12 char:9
    + Get-Help &lt;&lt;&lt;&lt;  TestVerbose
        + CategoryInfo          : MetadataError: (:) [Get-Help], MetadataException
        + FullyQualifiedErrorId : ParameterNameAlreadyExistsForCommand,Microsoft.PowerShell.Commands.GetHelpCommand</p>
  </blockquote>
  
  <p><strong>BUT,</strong> if I define the function like this [removing the parameter mandatory attribute], it works fine</p>
  
  <pre><code>function TestVerbose
  {
      param(
          [switch]$verbose,
          $p1
      )
      if($verbose)
      {
       Write-Verbose "Verbose Mode"
      }    
  }
  
  Get-Help TestVerbose
  </code></pre>
  
  <p>Any idea why such a behaviour ? I want to keep the mandatory switch and want the user to execute my function like this -</p>
  
  <blockquote>
    <p>TestVerbose -verbose</p>
  </blockquote>