Solved

Searching an imported .CSV in powershell

Posted on 2009-05-08
3
2,928 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

697 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