Home » excel » excel – How can I filter one column by name, then take value from another column?

excel – How can I filter one column by name, then take value from another column?

Posted by: admin May 14, 2020 Leave a comment


I am writing a script that will take a url and fetch its certificate information. I already have the certificate portion built out and need help with the excel piece.
I have all the urls stored in a big excel file.

I can import with

$xcel = Import-CSV C:\path\test.csv

then I can look at the differnt values in that sheet

$xcel | get-member

Now here I can see all of the different column names

What I want to do is filter by a column, then take another columns value.

For instance one column has a string of TYPE and the next has a string of URL

I want to sort by specific TYPEs and then take each of those types URL

How can I do this?

I am super new to Powershell (today) and I was trying something like

$xcel | foreach{if($_.TYPE -like "example"){$url = $_.URL}}

ideally I want to store each of the matching columns URL in a variable to then use in my Certificate function I built to retrieve the certificates.

How can I add additional filters for if and then take each returned URL and pass each one to the Certificate function?

How to&Answers:

You can do the following if you only care about the URL header:

$xcel.where({$_.TYPE -eq "example"}) | Foreach-Object {
    $url = $_.URL

If you only want to create a collection of URLs, then you can simply do the following. Then $urls will be an array of URLs or you can access them individually through a Foreach-Object or by index ($url[0], $url[1], etc.).

$urls = $xcel.where({$_.TYPE -eq "example"}).URL


The .where() method filters your $xcel array only displaying rows where the TYPE value is example. Those resulting rows are then piped into Foreach-Object. $URL will hold the value for the URL property for the current pipeline object ($_), which is the current row. Within that Foreach-Object script block, you can do your remaining actions with $URL.

An alternative solution would be a slight variation to the first. You can use the if statement approach, which may give you more flexibility with each row.

$xcel | Foreach-Object {
   if ($_.TYPE -eq "example") {
      $URL = $_.URL
   else {
      $URL = $null