1

我试图通过变量来理解powershell中@和$的真正含义:

PS C:\Windows\system32> Write-Host $str
string
PS C:\Windows\system32> Write-Host @str
s t r i n g

当我们创建一个哈希表时

PS C:\Windows\system32> $hash = @{key1 = 'value1'; key2 = 'value2'}
PS C:\Windows\system32> $hash

Name                           Value
----                           -----
key1                           value1
key2                           value2


PS C:\Windows\system32> @hash
At line:1 char:1
+ @hash
+ ~~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@hash' can be used only as an argument to a command. To reference variables in an expression use '$hash'.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : SplattingNotPermitted

PS C:\Windows\system32> Write-Host $host
System.Management.Automation.Internal.Host.InternalHost
PS C:\Windows\system32> Write-Host $hash
System.Collections.DictionaryEntry System.Collections.DictionaryEntry
PS C:\Windows\system32> Write-Host @hash
-key1: value1 -key2: value2

最后来这里了解一下变量前面使用@和$符号的区别

4

0 回答 0