The command gci env:ApiSecret | ConvertTo-Json
works to return a long string, the API secret for Twitter, which is truncated without the pipe to JSON.
However, the JSON is rather spammy.
Is there a “goldilocks” way to get the lengthy string value without the extraneous details?
(Unfortunately, gci env:
truncates the key)
3
Answers
Your statement does not strip anything away. However, for console display purpose, it truncate the output that you view in the console.
If you assign the result to a variable or pipe to a file, nothing will be truncated.
Therefore, my assumption on your question is that you want to view the result in the console without the console truncating your stuff there.
For that, you could write the results to the host yourself.
Here’s a simple example that do just that.
Result — As you can see, the path variable value is no longer truncated.
Get-ChildItem
is for retrieving all or a subset of items from a container. Note that it outputs an object withName
andValue
properties (substitutingPath
as another lengthy environment variable value)…Get-Item
yields the same result…Either way, the object retrieved is a
DictionaryEntry
……and when you pipe that to
ConvertTo-Json
it will include all kinds of undesirable properties from that class.In short, don’t use
ConvertTo-Json
for this. Since you know the exact item you want, just retrieve it directly using variable syntax…Equivalent code using the .NET API would be…
If you really wanted to use a
Get-*Item
cmdlet you’d just need to specify that it’s theValue
property you want using property syntax……or
Select-Object
…All of the above commands will output only a
[String]
containing the entirety of that environment variable value. I inserted trailing ellipses since showing my entirePath
value is not useful here; in practice, those commands will output the entire environment variable with no truncation.The simplest way to inspect the value of environment variables in full is to use the
$env:<varName>
(namespace variable notation) syntax, which in your case means:$env:ApiSecret
(if the variable name contains special characters, enclose everything after the$
in{...}
; e.g.,${env:ApiSecret(1)}
)That way, environment-variable values (which are invariably strings) that are longer than your terminal’s (console’s) width simply continue on subsequent lines.
To demonstrate:
With an 80-char. wide terminal, you’d see output as follows:
If you do want to use
Get-Item
(orGet-ChildItem
, which acts the same in this case), you have two options: