powershellherestring

How do I pass here-string as a parameter to a powershell script in a terminal?


I have a Powershell script named "file1.ps1" as below, which works perfectly fine:

$herestring = @"
State = Defined
FiscalWeek =
"@

$hash = ConvertFrom-StringData $herestring

$hash | Format-Table

This file gives the below output:

PS C:\temp> .\file1.ps1

Name                           Value                                                                                                                                     
----                           -----                                                                                                                                     
State                          Defined                                                                                                                                   
FiscalWeek                                                                                                                                                               

Now, I want to pass $herestring as a parameter to this file. So, the modified script looks like below:

param ($herestring)

$hash = ConvertFrom-StringData $herestring

$hash | Format-Table

So, when I run this in terminal of Powershell ISE or vscode, I am unable to paste or pass the $herestring since it has new line characters.

PS C:\Temp> .\file2.ps1 -herestring 

When I try to paste, each line is being treated as separate command.

PS C:\Temp> .\file2.ps1 -herestring @"
The string is missing the terminator: "@.
At line:0 char:0
PS C:\Temp> State = Defined
State : The term 'State' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again.
At line:1 char:1
+ State = Defined
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (State:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Temp> FiscalWeek =
FiscalWeek : The term 'FiscalWeek' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path 
was included, verify that the path is correct and try again.
At line:1 char:1
+ FiscalWeek =
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (FiscalWeek:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Temp> "@

How do I supply here-string as parameter in this case?

I tried to construct here-string as a single line within single quotes as well as three consecutive double quotes. Also, tried using backtick n and backtick r

Either it leads to syntax error or does not give expected results.


Solution

  • I don't know why your powershell executes at the occurrence of a new line instead of prompting for you to complete the command. It works for me. This is PS 5.1. Maybe update it if its an older version.

    enter image description here