r/PowerShell • • 17d ago

Question Importing specific rows from a .xslx-file to a .csv file

This is probably the most simple thing ever so apologies.

So I have an Excel worksheet and I want to import the data from specific rows, if column D in those rows has a certain value in it.

So using ImportExcel what I have is:

import-excel -path "filelocation" -WorksheetName "Worksheetname" | export-csv -path "exportlocation" -NoTypeInformation

What would be the missing bit to get the specific info I'm looking for? Again, I'm sorry for asking for what is probably a very simple solution.

13 Upvotes

5 comments sorted by

8

u/jungleboydotca 17d ago

Filter in the middle with Where-Object:

... |
Where-Object 'Column D' -eq 'certain value' |
...

...if your sheets have headers, use the name of the header in the column specification.

4

u/Ultimate_Harrier 17d ago edited 17d ago

Thank you very much! Though, when I added this, the resulting .csv ended up being completely empty, which didn't happen with the previous version. Did I mess up somewhere?

EDIT: No wait, nevermind, it was a mess up from my part. It works now, thank you again for your help!

5

u/ankokudaishogun 17d ago

And if you are using powershell 6+(Core), youi can skip the -NoTypeInformation switch.

Also, don't worry: your question falls in the "I don't know what to ask in first place" spot of legitimacy.

As added teaching moment, try to remember piping to Where-Object is the most common answer to "how to filter what I want to keep\remove?".

One thing to remember: many cmdlets have some type of built-in filtering option that is more efficient than piping.
This is often called "Left Filtering"(because the filtering happens on the "left" side of the command)
For example: Get-ChildItem has both switch so it only returns files(-File) or only folders(-Directory) and\or only results matching a simple pattern(-Filter).

Import-Excel itself has similar options:

in your specific example, you could use

    # For more about Splatting: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting  
    # Short version: you can make a hashtable with the parameters of a command, so it's easier to check them and automatize them.   
    $ImportExcelSplat = @{
    Path          = 'C:\whatever\FileLocation.xlsx'
    WorksheetName = 'WorksheetName'

    # Columns are identified by their number, not letter. 
    # A is 1, D is 4.
    StartColumn   = 4
    EndColumn     = 4

    # Rows use row numbers. Pretty straightforward.  
    StartRow      = 2
    EndRow        = 6

    # This decides what's the name of the resulting columns.  
    # If this is not set, the first cell will become the header name.  
    # Which, you know, it's perfectly fine if it *is* the header name.  
    HeaderName    = 'ColumnIwant'

    # As alternative, you can use "NoHeader = $true"
    # (you cannot use bot HeaderName and NoHeader)
    # It will basically generate a dedicated column name for the result.  
    # I suggest to use HeaderName if you don't already have headers in the sheet.   
    #NoHeader = $true
}


# With the selected properties, you are asking for:
# the 4th column, using the name ColumnIwant; 
# of the rows 2 to 6; 
# of the Worksheetname sheet; 
# of the C:\whatever\FileLocation.xlsx file
Import-Excel @ImportExcelSplat

You can still further filter the results by piping to Where-Object. Now, the difference with piping directly to Where-Object is probably going to be unnoticeable on a small file.
But on a 10k rows, 250 columns file? That's going to start showing.
And even more if it's not just one file but some dozen or thousand.

1

u/hemping0 17d ago

pretty much as straightforward as it gets when the headers are actually clean.

0

u/AGsec 17d ago edited 17d ago

You can use a loop that goes through each row, uses select-string to find the data, and if there is a match, then grab all of the data from that row and place it into an array for future export.

If you are going to downvote, please explain why. This is a place to learn. If my answer isn't good enough, technically or content wise, explain so I can do better.