What does the “@” symbol do in Powershell?

前端 未结 5 1904
青春惊慌失措
青春惊慌失措 2020-11-28 03:47

I\'ve seen the @ symbol used in PowerShell to initialise arrays. What exactly does the @ symbol denote and where can I read more about it?

5条回答
  •  有刺的猬
    2020-11-28 04:29

    You can also wrap the output of a cmdlet (or pipeline) in @() to ensure that what you get back is an array rather than a single item.

    For instance, dir usually returns a list, but depending on the options, it might return a single object. If you are planning on iterating through the results with a foreach-object, you need to make sure you get a list back. Here's a contrived example:

    $results = @( dir c:\autoexec.bat)
    

    One more thing... an empty array (like to initialize a variable) is denoted @().

提交回复
热议问题