Gonzalo Mendez
Gonzalo Mendez

Reputation: 11

PowerShell - How to add content to specific cell in CSV File

I'd like to seek your help on how to add content to a specific cell in a CSV file that is already with populated data, here's an example of my CSV file: CSV File with populated data

Here's an example of what I want to achieve:

UPN Skype? TV? Whiteboard? Description
room1 true true true Skype; TV; Whiteboard
room2 true false false Skype
room3 false true true TV; Whiteboard

If room1 has Skype, TV, and Whiteboard, then to create the description with these values. If any of these are false, skip them. Does it make sense?

How can I achieve this using PowerShell?

This is what I've got so far:

Import-Csv $MeetingRooms | ForEach-Object {
    # Description field on Column Number 19
    $col = 19

    if ($_.Skype -eq $true) {
        $var = 'Skype for Business;'
        Add-Content -path $_ -Value "$(","*$col)$var"
    }elseif ($_.TV -eq $true) {
        $var = 'TV;'
        Add-Content -path $_ -Value "$(","*$col)$var"
    }elseif ($_.Whiteboard -eq $true) {
        $var = 'Whiteboard;'
        Add-Content -path $_ -Value "$(","*$col)$var"
    }
}

I am missing something big here which clearly can't see.

Thank you very much, Gonzalo

Upvotes: 1

Views: 1094

Answers (1)

mklement0
mklement0

Reputation: 437121

The following assumes that the column with index 19 is named CurrentFacilities.

Import-Csv $MeetingRooms | ForEach-Object {
  $obj = $_
  $obj.CurrentFacilities =
    ($obj.CurrentFacilities -split ';\s*') +
    ('Skype', 'TV', 'Whiteboard').Where({ $obj.$_ -eq 'true' }) -join '; '
  $obj # Output the modified object.
}

Upvotes: 1

Related Questions