Challenge

You have a large CSV file with many objects.  You want to work with a subset from the file.  You want the subset to be from midway in the file. You want a range such as the 150th object to the 400th object. 

You do NOT want just the first or last number of objects.

Solution

Use piping and Select-Object to pull the range of objects you want.

Your file has a collection with 1,000 objects.  You want objects 150 to 400.  

# Variables:
$StartRange = 150
$EndRange = 400
$SubsetRange = $EndRange - $StartRange
$ImportFileTotal = (Import-CSV .\YourImportFile.csv).Count
$ImportFileEnd = $ImportFileTotal - $StartRange
# Process:
Import-CSV .\YourImportFile.csv |
Select-Object -Last $ImportFileEnd  |
Select-Object -First $SubsetRange

How this works

The solution uses successive use of Select-Object and the -First and -Last parameters.  

Import-CSV creates an object collection with all the objects from the import file.

The first Select-Object creates a subset of all objects starting with the object at the $StartRange  position.  All objects prior to the $StartRange object are omitted from the output object

The second Select-Object creates a subset of the first objects up to the $SubsetEndrange.  The output object has the number of objects that are within the desired range from the original collection start object to the end object.

Microsoft Documentation-Select-Object

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>