tilbard
asked on
Searching an imported .CSV in powershell
I'm trying to import a simple .CSV file into Powershell using import-csv, then search it for a specific string, and display the description of the object that matches the string.
CSV File looks like this:
Name,Description
Server1,Server1 description
Server2,Server2 Description
So basically, I want to search through the list for server2, and display server2's description. It doesn't seem to like how I'm doing it however, because it doesn't display anything. Attached is the code I'm trying to use to do it.
Thanks in advance!
CSV File looks like this:
Name,Description
Server1,Server1 description
Server2,Server2 Description
So basically, I want to search through the list for server2, and display server2's description. It doesn't seem to like how I'm doing it however, because it doesn't display anything. Attached is the code I'm trying to use to do it.
Thanks in advance!
cls
$Serverlist = (Import-Csv "d:\serverlist.csv")
Write-Host ($Serverlist.description | where{$serverlist.name -match "server2"})
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You didn't need a ForEach loop, it really depends on what you're trying to see :) If you wanted all Descriptions as simple strings you would need ForEach. e.g.:
$ServerList | %{ $_.Description }
Chris
This will return the Description if you have multiple matches for your Where-Object search.
$ServerList = Import-CSV "d:\serverlist.csv"
$ServerList | ?{ $_.Name -Match "server2" } | Select-Object Description
Or if the search returns a single result you can access just that element (implicit conversion from array of objects to object):
($ServerList | ?{ $_.Name -Match "server2" }).Description
Chris