4

I have to display four columns but three of them each time, the column reference doesn't need to be displayed for a problem of space. This code allows to display cells which aren't in the column reference. When the cells match, the cell in the other columns disappears.

file:

  ComputerName  OtherComputerName   OtherComputer   AndAgain
    infra-1     infra-852            infra-2        infra-99
    infra-98    infra-85             infra-44       infra-23
    infra-5     infra-8              infra-1        infra-10
    infra-2     infra-55             infra-8        infra-70
    infra-62    infra-5              infra-852      infra-5

script:

$csv = Import-Csv .\test1.csv -Delimiter ';'

$ref = @($csv.ComputerName)
foreach ($row in $csv) {
  foreach ($col in 'OtherComputerName', 'OtherComputer', 'AndAgain') {
    if ($ref -contains $row.$col) { $row.$col = '' }
  }
}

$csv

the result on PS: I would like to not display the ComputerName column and retire the space between the cells.

OtherComputerName OtherComputer AndAgain
----------------- ------------- --------
infra-852                       infra-99
infra-85          infra-44      infra-23
infra-8                         infra-10
infra-55          infra-8       infra-70
                  infra-852             

the expected result:

OtherComputerName OtherComputer AndAgain
----------------- ------------- --------
infra-852         infra-44      infra-99
infra-85          infra-8       infra-23
infra-8           infra-852     infra-10
infra-55                        infra-70
3
  • Use Where-Object to filter output based on whatever criteria you want, and Select-Object to output only the columns you want. Commented Jun 19, 2017 at 14:37
  • Can you show me an example pls i'm a little bit lost. I don't know how to use it. Commented Jun 19, 2017 at 14:42
  • Your question is not clear on what conditions you are trying to use. Commented Jun 19, 2017 at 14:53

2 Answers 2

6

You can either list all columns/properties to include

$csv | Select-Object OtherComputerName,OtherComputer,AndAgain

or use -ExcludeProperty to remove named ones - but then there must be a least one -Property (position 0 doesn't have to be written out, simply use an asterisk for all)

$csv | Select-Object * -ExcludeProperty ComputerName 

Sample output here exactly matching your requirements:

OtherComputerName OtherComputer AndAgain
----------------- ------------- --------
infra-852                       infra-99
infra-85          infra-44      infra-23
infra-8                         infra-10
infra-55          infra-8       infra-70
                  infra-852

Not a table but a single shrinked col:

$csv|select Othercomputer|where Othercomputer -ne ''

Sample output:

OtherComputer
-------------
infra-44
infra-8
infra-852
Sign up to request clarification or add additional context in comments.

14 Comments

When I exclude the column there still a big space between some cells. Is it possible to retire it ?
Append |Format-Table -auto
$csv | Select-Object * -ExcludeProperty ComputerName |Format-Table -auto like this ? If it is, it does nothing
The columns can't be smaller than the headings.
So, It is impossible to exclude cells which are matching with the first column ?
|
0

This should do your work:

$csv = Import-Csv .\test1.csv -Delimiter ';'

$ref = @($csv.ComputerName)
foreach ($row in $csv) {
  foreach ($col in 'OtherComputerName', 'OtherComputer', 'AndAgain') {
    if ($ref -contains $row.$col) { $row.$col = '' }
  }
}

$csv | Select-Object ComputerName , AndAgain

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.