Search code examples
jsonpowershellenvironment-variablessystem-administration

How to get a lengthy string environment variable in full?


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)


Solution

  • Get-ChildItem is for retrieving all or a subset of items from a container. Note that it outputs an object with Name and Value properties (substituting Path as another lengthy environment variable value)...

    PS> gci env:Path
    
    Name                           Value
    ----                           -----
    Path                           C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\WINDO...
    

    Get-Item yields the same result...

    PS> gi env:Path
    
    Name                           Value
    ----                           -----
    Path                           C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\WINDO...
    

    Either way, the object retrieved is a DictionaryEntry...

    PS> gi env:Path | Get-Member
    
    
       TypeName: System.Collections.DictionaryEntry
    
    Name          MemberType    Definition
    ----          ----------    ----------
    Name          AliasProperty Name = Key
    Equals        Method        bool Equals(System.Object obj)
    GetHashCode   Method        int GetHashCode()
    GetType       Method        type GetType()
    ToString      Method        string ToString()
    PSDrive       NoteProperty  PSDriveInfo PSDrive=Env
    PSIsContainer NoteProperty  bool PSIsContainer=False
    PSPath        NoteProperty  string PSPath=Microsoft.PowerShell.Core\Environment::path
    PSProvider    NoteProperty  ProviderInfo PSProvider=Microsoft.PowerShell.Core\Environment
    Key           Property      System.Object Key {get;set;}
    Value         Property      System.Object Value {get;set;}
    

    ...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...

    PS> $env:Path
    C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;...
    

    Equivalent code using the .NET API would be...

    PS> [Environment]::GetEnvironmentVariable('Path')
    C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;...
    

    If you really wanted to use a Get-*Item cmdlet you'd just need to specify that it's the Value property you want using property syntax...

    PS> (gi env:Path).Value
    C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;...
    

    ...or Select-Object...

    PS> gi env:Path | Select-Object -ExpandProperty 'Value'
    C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;...
    

    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 entire Path value is not useful here; in practice, those commands will output the entire environment variable with no truncation.