Why declare a variable type for Powershell parameters?

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 8 months ago.

  • Author
    Posts
  • #5529

    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.

You must be logged in to reply to this topic.