Solved

Searching an imported .CSV in powershell

Posted on 2009-05-08
3
2,674 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
Comment Utility
Needed a foreach loop in there to process the whole of $serverlist.
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

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
Comment Utility

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

10 Experts available now in Live!

Get 1:1 Help Now