What is the difference between using @ and $ in front of variable in powershell?

210 Views Asked by At

I tried to pass the variables to understand the real meaning of the @ and $ in the powershell as :

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

and when we create an hash table like

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

Finally, I'm here to know about the difference between the use of @ and $ sign in front of the variables

0

There are 0 best solutions below