Inconsistency in Write-Output "$obj" and directly printing $obj in Powershell -


this dummy.json file

{     "key1":"value1",     "key2":"value2" } 

i'm reading contents of file variable , outputting them

c:> $obj = get-content .\dummy.json c:> $obj {         "key1":"value1",         "key2":"value2" } c:> write-host "$obj" {       "key1":"value1",        "key2":"value2" } 

i know get-content doesn't preserve line breaks , joins " ". powershell keep text formatting when reading in file

but why there inconsistency in above 2 outputs ? guess write-host doing job correctly. or wrong ?

it's not get-content joins lines (its output array of strings), putting variable in double quotes ("$obj"). can avoid joining lines yourself:

write-host ($obj -join "`n") write-host ($obj | out-string) write-host $($ofs="`n"; "$obj") 

another option read file directly single string, e.g. (requires powershell v3 or newer):

$obj = get-content .\dummy.json -raw 

or this:

$obj = [io.file]::readalltext('.\dummy.json') 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -