Solved

Searching an imported .CSV in powershell

Posted on 2009-05-08
3
3,061 Views
Last Modified: 2012-06-27
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!
cls
$Serverlist = (Import-Csv "d:\serverlist.csv")
Write-Host ($Serverlist.description | where{$serverlist.name -match "server2"})

Open in new window

0
Comment
Question by:tilbard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
tilbard earned 0 total points
ID: 24336038
Needed a foreach loop in there to process the whole of $serverlist.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24336068

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
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24336085

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
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question