Home > OS >  How to Stop powershell form Converting the variable assigment of a boolean to Uppercase letter
How to Stop powershell form Converting the variable assigment of a boolean to Uppercase letter

Time:03-26

I am trying to format a json and do dynamic variable assignment but once the boolean value is assigned powershell is changing the casetype for the first letter to uppercase. I still want to maintain the case type for my input values both lowercase and uppercase as it is in my json file.

Any help?

      {

          "input": true,
          "variable": "Relative path",
       }

  $path= "L:\test\parameter.json"
  $json = Get-Content $path | ConvertFrom-Json
  foreach ($data in $json.PSObject.Properties) { New-Variable -name $($data.name) -value $($data.value) -Force}
  
  echo $input
  True   ->>> I want it to be "true" and the value of variable to still be "Relative Path"

CodePudding user response:

Generally, you mustn't use $input as a custom variable, because it is an automatic variable managed by PowerShell.

Leaving that aside, ConvertFrom-Json converts a true JSON value - a Boolean - into the equivalent .NET Boolean. The representation of this value in PowerShell is $true.

Printing this value to the console (host) effectively calls its .ToString() method in order to obtain a string representation, and that string representation happens to start with an uppercase letter:

PS> $true
True 

If you need an all-lowercase representation, call .ToString().ToLower(), or, for brevity, use an expandable string and call .ToLower() on it:

PS> "$true".ToLower() # In this case, the same as $true.ToString().ToLower()
true
  • Related