Solved

Searching an imported .CSV in powershell

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

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 70

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now