What does the “@” symbol do in Powershell?

前端 未结 5 1935
青春惊慌失措
青春惊慌失措 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:36

    While the above responses provide most of the answer it is useful--even this late to the question--to provide the full answer, to wit:

    Array sub-expression (see about_arrays)

    Forces the value to be an array, even if a singleton or a null, e.g. $a = @(ps | where name -like 'foo')

    Hash initializer (see about_hash_tables)

    Initializes a hash table with key-value pairs, e.g. $HashArguments = @{ Path = "test.txt"; Destination = "test2.txt"; WhatIf = $true }

    Splatting (see about_splatting)

    Let's you invoke a cmdlet with parameters from an array or a hash-table rather than the more customary individually enumerated parameters, e.g. using the hash table just above, Copy-Item @HashArguments

    Here strings (see about_quoting_rules)

    Let's you create strings with easily embedded quotes, typically used for multi-line strings, e.g.:

    $data = @"
    line one
    line two
    something "quoted" here
    "@
    

    Because this type of question (what does 'x' notation mean in PowerShell?) is so common here on StackOverflow as well as in many reader comments, I put together a lexicon of PowerShell punctuation, just published on Simple-Talk.com. Read all about @ as well as % and # and $_ and ? and more at The Complete Guide to PowerShell Punctuation. Attached to the article is this wallchart that gives you everything on a single sheet:

提交回复
热议问题