Created
May 13, 2021 22:05
-
-
Save istairbn/5b2864f55b488596eb21534b31cbff6e to your computer and use it in GitHub Desktop.
CSV_Filter.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
CSV_Filter takes a CSV and returns a new one based off a filter | |
.DESCRIPTION | |
A simple script to return a filtered CSV. The matching is wildcard (so X will find X AND exhibit) | |
Please note this is not robust. If you give it a column that doesn't exist, output is empty. It will fail if the input CSV doesn't exist | |
.PARAMETER Input_CSV | |
The path to the CSV you want to ingest and filter. You must specify this. | |
.PARAMETER Output_CSV | |
The path where you want the output CSV to be. Defaults to output.csv. | |
.PARAMETER Filter_Column | |
The column in the spreadsheet you want to base the filter on. So if you have a column "ID" to filter on. | |
.PARAMETER Filter_Match | |
The string you want to match upon. Defaults to TX | |
.EXAMPLE | |
./CSV_Filter -Input_CSV ./MyBigCSV.csv -Output_CSV MyCLeaned.csv -Filter_Column ID -Filter_Match 1234 | |
.NOTES | |
Author: Benjamin Newton | |
Date: May 13, 2021 | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True)] | |
[ValidateScript( { Test-Path $_ } )] | |
$Input_CSV = ".\Input.csv", | |
[Parameter()] | |
$Output_CSV = ".\Output.csv", | |
[Parameter(Mandatory=$True)] | |
$Filter_Column, | |
[Parameter(Mandatory=$False)] | |
$Filter_Match = "TX" | |
) | |
$Input_Object = Get-Content $Input_CSV | ConvertFrom-Csv | |
$Output_Object = @() | |
ForEach( $Row in $Input_Object ){ | |
If ($row.$($Filter_Column) -match $Filter_Match){ | |
$Output_Object += $Row | |
} | |
} | |
$Output_Object | ConvertTo-Csv -NoTypeInformation | Out-File -FilePath $Output_CSV -Encoding utf8 -Force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment