-1

I'm sure this is quite easy but I can't find a way to format a semicolon-delimited string as a list or object, with as generic method as possible.

Running the command displays the following:

PS> Get-childitem -path Env:\Path

Name                           Value
----                           -----
Path                           Value1;Value2;Value3 etc...

Expected result is:

Name                               Value
----                               ----
Path                               Value1
Path                               Value2
Path                               Value3
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Tapper
  • 5
  • 1
  • 7
  • Not quite what you were after but produces an array of the values: `(Get-ChildItem -Path Env:\Path | Select-Object -ExpandProperty Value) -split ";"` – henrycarteruk Feb 19 '18 at 17:23

3 Answers3

2

You can split the value and use ex. Select-Object with calculated properties to add the Name-property.

$env:Path.Split(';',[System.StringSplitOptions]::RemoveEmptyEntries) | Select-Object @{n="Name";e={"Path"}}, @{n="Value";e={$_}}

Or using cmdlet

(Get-Item Env:\Path | Select-Object -ExpandProperty Value).Split(';',[System.StringSplitOptions]::RemoveEmptyEntries) |
Select-Object @{n="Name";e={"Path"}}, @{n="Value";e={$_}}

If you want to support multiple variables:

Get-ChildItem Env:\Path | ForEach-Object {
    $name = $_.Name
    $_.Value -split ';' | Where-Object { $_ } | Select-Object @{n="Name";e={$name}}, @{n="Value";e={$_}}
}

Output:

Name Value
---- -----
Path C:\WINDOWS\system32
Path C:\WINDOWS
Path C:\WINDOWS\System32\Wbem
Path C:\WINDOWS\System32\WindowsPowerShell\v1.0\
....

(I use RemoveEmptyEntries and Where-Object { $_ } to avoid empty entries as I had a trailing ; in my own Path-variable)

Frode F.
  • 52,376
  • 9
  • 98
  • 114
1

To get the exact output you want:

$env:Path -split ';' -ne '' |
  Select-Object @{ n = 'Name'; e = { 'Path '} }, @{ n = 'Value'; e = { $_ } }
  • $env:Path -split ';' splits the value of $env:Path (the more efficient equivalent of Get-Item env:\Path) into an array of strings by separatory ;.

  • -ne '' removes empty entries from the resulting array.

  • The Select-Object command constructs a 2-property object for each array entry that by default displays as a table:

    • The @{ ... } arguments are calculated properties that define the .Name and .Value properties for the output object, using a hashtable format that is explained in this answer.
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

Following code snippet may be useful:

("Name          ->      Value`r`n--------------------------------------------------");(Get-ChildItem -Path Env:\Path | Select-Object -ExpandProperty Value) -split ";" | foreach {"Path   ->   " + $_}

It will produce following output:

Name          ->      Value
--------------------------------------------------
Path   ->   C:\ProgramData\Oracle\Java\javapath
Path   ->   C:\Program Files\Microsoft MPI\Bin\
Path   ->   C:\WINDOWS\system32
Path   ->   C:\WINDOWS
Path   ->   C:\WINDOWS\System32\Wbem
cse
  • 4,066
  • 2
  • 20
  • 37