title: complex default values for powershell script Params
body: <p>I would like to set a default <code>Param</code> value that must be determined at script runtime (cannot be hardcoded).</p>
  <p>Given powershell script <code>file.ps1</code></p>
  <pre><code>&lt;#
  .PARAMETER arg1
      The first argument. Defaults to ???
  #&gt;
  Param (
      [string] $arg1,
  )
  </code></pre>
  <p>I would like:</p>
  <ol>
  <li>Set <code>$arg1</code> to a value that must be determined when the script runs, for example, the host IP address.</li>
  <li>Print the default value of <code>$arg1</code> within the <code>.PARAMETER arg1</code> help message.</li>
  </ol>
  <p>Typically I might add</p>
  <pre><code>$arg1_default = (Test-Connection -ComputerName &quot;www.google.com&quot; -Count 1).Address.IPAddressToString
  
  &lt;#
  .PARAMETER arg1
      The first argument. Defaults to $arg1_default.
  #&gt;
  Param (
      [string] $arg1 = $arg1_default,
  )
  </code></pre>
  <p>However, in Powershell, the <code>Param</code> statement must be the first processed statement. If I add any complex statements before <code>Param</code> statement then it results in an error:</p>
  <pre><code>PS&gt; .\file.ps1
  Param: file.ps1:9
  Line |
     9 |  Param (
       |  ~~~~~
       | The term 'Param' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the
       | name, or if a path was included, verify that the path is correct and try again.
  </code></pre>
  <br />
  <p>How do I set default values for a powershell script <code>Param</code> ?</p>
  <br />
  <br />
  <p>I'm using Powershell 7.</p>