I'm new to scripting and struggling with implementing some concepts. I've got a PowerShell script working that will take in input from the user and store it into several variables. It will then look at a .csv file and only output rows where the several inputs from the user is not like the value in a specified column.
For example, if I've got a .csv file that contains:
value1, value2, value3
data1, data2, data3
data1, data2, data3
data1, data5, data3
data1, data2, data3
And I ask to output the rows where value2 is not like data5 I get the following as output:
value1, value2, value3
data1, data2, data3
data1, data2, data3
data1, data2, data3
Here's the current code I have that is working:
$path = Read-Host 'What is the file share path?'
$filteredvalue = Read-Host 'What value would you like to filter out?'
$filteredvalue2 = Read-Host 'What second value would you like to filter out?'
$filteredvalue3 = Read-Host 'What second value would you like to filter out?'
$filteredvalue4 = Read-Host 'What second value would you like to filter out?'
$filteredvalue5 = Read-Host 'What second value would you like to filter out?'
$filteredvalue6 = Read-Host 'What second value would you like to filter out?'
$filteredvalue7 = Read-Host 'What second value would you like to filter out?'
$file1 = $path
$filtered = Import-Csv -Path $file1 | where {$_.IdentityReference -notlike $filteredvalue -and $_.IdentityReference -notlike $filteredvalue2 -and $_.IdentityReference -notlike $filteredvalue3 -and $_.IdentityReference -notlike $filteredvalue4 -and $_.IdentityReference -notlike $filteredvalue5 -and $_.IdentityReference -notlike $filteredvalue6 -and $_.IdentityReference -notlike $filteredvalue7}
$file2 = "${path}_filtered.csv"
$filtered | Export-Csv -Path $file2 -NoTypeInformation
This works but is not the best solution as I have to specify in the code how many values I'll be filtering out. I would like to use arrays to prevent having to create all the variables for the values.
Here's what I've tried so far but no luck:
$path = Read-Host 'What is the file share path?'
$Reponse = 'Y'
$filteredvalue = $Null
$filteredvalues = @()
Do
{
$filteredvalue = Read-Host 'What value would you like to filter out?'
$Response = Read-Host 'Would you like to filter out additional values? (y/n)'
$filteredvalues += $filteredvalue
}
Until ($Response -eq 'n')
$file1 = $path
$filtered = Import-Csv -Path $file1 | where {for($i = 0; $i -le $filteredvalues.GetUpperBound(0); $i++) {$_.IdentityReference -ne $filteredvalues[$i]}}
$file2 = "${path}_filtered.csv"
$filtered | Export-Csv -Path $file2 -NoTypeInformation
The output remains the same and nothing gets filtered out.