2

I have a CSV file, Devices.csv, containing IP Address, DeviceName, SerialNumber, MAC Address, UserName.

The Users column in all the rows of Devices.csv is prepopulated with a value [Unknown]

The code...

{Import-CSV Devices.csv | Where-Object {$_.IPAddress -eq '192.168.2.124'} | Select-Object -last 1 | FT  IPAddress, devicveName, SerialNumber,  MACAddress, User -AutoSize }

....outputs

IPAddress     deviceName     SNumber     MACAddress   User
---------     -----          -------     ----------   ----
192.168.2.124 ComputerA      1ABCDEFG    00xxYYbbCCdd [Unknown]   

I want to be able to replace the [Unknown] text with a Username I have retrieved from a different source. Can I update just the User column on this line in Devices.csv using powershell and keep the rest of the CSV file intact ?

Thanks, Brian

2 Answers 2

1
$csv = Import-CSV Devices.csv
$csv | Where-Object {$_.IPAddress -eq '192.168.2.124'} | Select-Object -Last 1 | ForEach-Object {$_.User = $user}
$csv | Export-Csv Devices.csv -NoTypeInformation
Sign up to request clarification or add additional context in comments.

1 Comment

Shay, Exactly what I wanted, Thanks Brian
1

Try this:

$otheruser = '...'

Import-Csv Devides.csv | % {
  if ($_.User -eq '[Unknown]') { $_.User = $otheruser }
  $_
} | Export-Csv Divices_modified.csv -NoTypeInformation

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.