Why declare a variable type for Powershell parameters?

Welcome Forums General PowerShell Q&A Why declare a variable type for Powershell parameters?

Viewing 0 reply threads
  • Author
    Posts
    • #5529
      Member
      Topics: 1562
      Replies: 0
      Points: 0
      Rank: Member

      by BustedFlush at 2013-02-09 10:21:48

      In this article regarding Powershell parameters, Don Jones writes:
      (http://technet.microsoft.com/en-us/maga ... 54301.aspx)

      "You can tell Windows PowerShell to expect these parameters, collect them from the command line, and put their values into variables within your script or function. That makes dealing with input easy and efficient.

      You just have to know how to declare your parameters. The simplest means of doing so is the param block:

      Param(
      [string]$computerName,
      [string]$filePath
      )"

      And that's just great, but I have a question; why did declare the variable types like that? ([string])

      Is it necessary?
      Does it provide any added functionality or prevent potential problems?

      by DonJ at 2013-02-09 12:45:06

      It isn't necessary, but it (a) ensures I get a string – as opposed to a Service object or something else – in the parameter, and (b) helps document what the parameter expects for anyone running the command. PowerShell uses that to construct a basic default help display, for example.

Viewing 0 reply threads
  • The topic ‘Why declare a variable type for Powershell parameters?’ is closed to new replies.