KamilCuk
KamilCuk

Reputation: 141758

How not to trim lines when printing environment variables in powershell?

To print environment in PowerShell I can do, according to this:

dir env:

So I did, and for example for Path and for other environment variables longer then my window I see:

 Path                      C:\Python39\Scripts\;C:\Python39\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;...

I do not want ..., I want to see the full length anything there is value of environment variable of all variables.

I tried to read: https://superuser.com/questions/1049531/how-to-fix-truncated-powershell-output-even-when-ive-specified-width-300 and tried to cd env: -Width 1000, or tried cd env: and then Get-ChildItem -Width 1000, it does not work, and searched google but with no success. The dir env: | Out-String -width 999 results in an endless amount of empty lines. What works is dir env: | cat, but then the names of variables disappear.

Is there any way I can view not-truncated values of all environment variables with variable names?

Upvotes: 1

Views: 718

Answers (2)

mclayton
mclayton

Reputation: 10105

out-string behaves slightly differently on Windows PowerShell versus PowerShell Core - it looks like PowerShell Core uses -Width as a maximum width whereas Windows PowerShell pads each line to the specified width so you're seeing each environment variable separated by a lot of spaces.

PowerShell Core

PS 7.1.3> dir env: | out-string -width 9999

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\Mike\AppData\Roaming
CommonProgramFiles             C:\Program Files\Common Files
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
CommonProgramW6432             C:\Program Files\Common Files

Windows PowerShell

PS 5.1> dir env: | out-string -width 9999

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData




APPDATA                        C:\Users\Mike\AppData\Roaming




CommonProgramFiles             C:\Program Files\Common Files




CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files




CommonProgramW6432             C:\Program Files\Common Files

(padding not to scale!)

It's a bit ugly, but if you want to make Windows PowerShell output look the same you can do something like this:

PS> ((dir env: | format-table | out-string -width 9999) -split [System.Environment]::NewLine).Trim() -join [System.Environment]::NewLine

Basically, split the output into lines, trim them and then join them back together again.

Upvotes: 1

Manuel Batsching
Manuel Batsching

Reputation: 3606

Taking your question literally, if you just want to view the env variables with their untruncated values on the console, you can simply format the output as list:

Get-ChildItem env: | Format-List

This will display the information in the following fashion and insert visual line breaks if the value is longer than the available space:

Name  : PSModulePath
Value : C:\Users\{and so on}

Upvotes: 2

Related Questions