When you try to print or access the Hashes in PowerShell v1.0 using @ operator, you’ll receive the error message detailed in this article below. In PowerShell v1.0, you instead need to use $ operator to refer to an hash variable.
Code:
$email = @{}
$email.Add("From", "TestSender")
$email.Add("To", "[email protected]")
$email.Add("Subject", "Test mail from PowerShell 1.0")
$email.Add("Body", “Hi There, Reply me back”)
Error:
PS C:Test> powershell C:Testtest-hases.ps1
Unrecognized token in source text.
At C:packagingtest-hases.ps1:9 char:13
+ write-host @ <<<< email
PS C:Test>
Fix:
In PowerShell v1.0, a hash cannot be print using the @<hashname> operator. Instead, you can print the output by specifying the Hash as $<hashname>
Verification:
$email = @{}
$email.Add("From", "TestSender")
$email.Add("To", "[email protected]")
$email.Add("Subject", "Test mail from PowerShell 1.0")
$email.Add("Body", “Hi There, Reply me back”)PS C:> powershell C:Testtest-hases.ps1
Name Value
—- —–
Body Hi There, Reply me back
Subject Test mail from PowerShell 1.0
To [email protected]
From TestSenderPS C:>