58150

Display only the columns needed PowerShell

Question:

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

Answer1:

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

Answer2:

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

Recommend

  • chef-solo getting logs from a bash script
  • Why custom domain “myapp.com” which replaces “myapp.appspot.com” took so long to load when comparing
  • Exclude cells which aren't displayed with Powershell
  • In PHP, how to preserve 1st part of a link, keep 2nd and dynamically change the rest
  • table with scrollable body and colgroup does not work
  • How to scroll to a particular div after reloading the page when the submit button is clicked?
  • Rails - Cocoon gem - Nested Forms
  • Generic EditorTemplate for an Enum with Text Select Option Value
  • WCF web service returning json format data
  • Primefaces Dialog Framework — dialogReturn event from menuitem
  • Slow Performance When Reading Excel With Microsoft.office.Interop.Excel
  • How can I transform an array of characters with a few lines of code to a data.frame?
  • “stack level too deep” When Processing Carrierwave Image Versions in Nested Form
  • ng-bootstrap - ngbtooltip not working with [(ngModel)]
  • Sortable Columns In scala.swing.Table
  • DataTables+RequireJS: Cannot read property 'defaults' of undefined
  • R Error - cannot change value of locked binding for 'df'
  • Compare a column between 2 csv files and write differences using Python
  • href inside href [duplicate]
  • How to remove comma or any characters from Python dataframe column name
  • SSH in Bash Script Messing Up File Read
  • XSLT foreach repeating nodes to flat
  • List images(01.png) and descriptions(01.txt) from directory
  • gspread or such: help me get cell coordinates (not value)
  • Textfile Structure (tables)
  • D3 nodes and links from JSON with nested arrays of children
  • DomPDF {PAGE_NUM} not on first page
  • Javascript simulate pressing enter in input box
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Importing jscolor library in angular 2
  • Why winpcap requires both .lib and .dll to run?
  • align graphs with different xlab
  • Return words with double consecutive letters
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • A cron job substitute?
  • Python: how to group similar lists together in a list of lists?
  • Can't mass-assign protected attributes when import data from csv file
  • Busy indicator not showing up in wpf window [duplicate]
  • Unable to use reactive element in my shiny app