read-host to variable with spaces not working

This topic contains 8 replies, has 5 voices, and was last updated by Profile photo of vcloudguy vcloudguy 1 year, 10 months ago.

  • Author
  • #29056
    Profile photo of vcloudguy

    I am trying to read-host text with spaces and save it into a variable in powershell.

    However when the variable is passed as an argument to another command then it ignore the space.

    $Name = read-host "enter text here"
    enter text here: hello world

    ec2-create-tags %1 -t "Name=%2" -t "Server=%3"

    In the above command it puts Name= Hello and Server= World

    I would like to store Name= Hello World

    I am not sure if there is any formatting error? I tried the backtick " backtick " option but it didn't work either.

    Any help would be appreciated.



  • #29058
    Profile photo of Don Jones
    Don Jones

    Why don't you just use "Name=$Name" if that's what you want? I guess I'm confused why you're using the % tokens.

  • #29059
    Profile photo of vcloudguy

    Thanks for the response Dan. When I use "Name=$Name" then I get the return value also as Name=$Name

    I am using %tokens because the ec2-create-tags command is called from a batch file by another powershell script that reads the user inputs with read-host variables.

  • #29060
    Profile photo of vcloudguy

    To be more specific this is the command that I am calling from a powershell script


    And I use tokens as variables from my powershell script.
    "c:\bin\nameInstanceTagVal.cmd $instanceID $instanceName $Owner $Purpose"

    this is the command that I am running from the batch file
    ec2-create-tags %1 -t "Name=%2" -t "Owner=%6" -t "Purpose=%7"

    Do you have any other ideas?

  • #29061
    Profile photo of Bob McCoy
    Bob McCoy

    Under "Any help would be appreciated" ... get batch files completely out of the mix. You are creating unnecessary complications. Unless there is a TRULY compelling reason, it should be all PowerShell.

  • #29062
    Profile photo of vcloudguy

    Thanks for the response Bob. The reason for using the batch file is because the batch file is also setting up the environment variables for java.

    ec2-create-tags is a command that is run via SOAP call using java and my powershell script is calling other batch files which uses the SOAP calls.

  • #29082
    Profile photo of Sebastian Neumann
    Sebastian Neumann

    You can also set environment variables in Powershell and just do everything in Powershell which would reduce the complexity a lot.

  • #29147
    Profile photo of Max Kozlov
    Max Kozlov

    if you use powershell only for read-host...

    ... not use powershell at all and use "set /p cmd" command

  • #29150
    Profile photo of vcloudguy

    Thanks for the response Megamorf. I did exactly that by moving the code from batch files into the powershell script to eliminate the complexity. Also, I used the syntax in powershell as described in the command's help that I was trying to execute.

    If you're using the command line tools on a Windows system, you might need to use quotation marks (for example, "key=value"). If you're using Windows Powershell, you might need to use a second set of quotation marks, escaped with backticks (for example, "`"key=value`"").

    Thanks for all your help.

You must be logged in to reply to this topic.