You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助PowerShell脚本:CSV列过滤及数组值筛选并保存结果

Solution for Filtering CSV Data with an Array of Values

Hey there! Great job making it this far with PowerShell—you’re picking up scripts really quickly, which is awesome. Let’s tackle your goal of filtering your CSV data using an array of specified values and saving the results to a new file.

First, Let’s Optimize Your Existing Script

Your current script works, but we can make it cleaner and avoid unnecessary file writes (like creating file2.csv when we can process the data directly in memory). Here’s a streamlined version that still handles the initial download and skips the unwanted first line:

<# 此脚本用于下载资产分析服务器列表及其对应的站点地址和国家信息,并保存至桌面路径下的CSV文件 #>
# Get the desktop path dynamically (avoids hardcoding usernames)
$desktopPath = [Environment]::GetFolderPath("Desktop")
$file1Path = Join-Path -Path $desktopPath -ChildPath "file1.csv"

# Download the CSV from your target URL
Invoke-WebRequest -Uri "Link to URL" -OutFile $file1Path

# Import the CSV, skip the first line, and assign headers directly
$csvData = Get-Content $file1Path | Select-Object -Skip 1 | ConvertFrom-Csv -Header "#Active Servers", "Street Address", "City", "Country"

Adding Array-Based Filtering

Now, let’s implement the array filtering you need. First, define an array of the values you want to keep (e.g., specific countries). Then use the -contains operator to check if a row’s Country matches any value in that array:

# Define your array of target countries (add/remove values as needed)
$targetCountries = @("UNITED STATES", "CANADA", "UNITED KINGDOM")

# Filter the data to only include rows where Country is in the target array
$filteredData = $csvData | Where-Object { $targetCountries -contains $_.Country }

# Save the filtered data to a new CSV file on your desktop
$filteredFilePath = Join-Path -Path $desktopPath -ChildPath "filtered_servers.csv"
$filteredData | Export-Csv -Path $filteredFilePath -NoTypeInformation

Full Combined Script

Putting it all together, here’s the complete script that handles every step in one flow:

<# 此脚本用于下载资产分析服务器列表,过滤指定国家的条目,并保存至桌面路径下的CSV文件 #>
# Get desktop path dynamically (works for any user)
$desktopPath = [Environment]::GetFolderPath("Desktop")
$sourceCsvPath = Join-Path -Path $desktopPath -ChildPath "file1.csv"
$filteredCsvPath = Join-Path -Path $desktopPath -ChildPath "filtered_servers.csv"

# Download the source CSV
Invoke-WebRequest -Uri "Link to URL" -OutFile $sourceCsvPath

# Define countries to filter for
$targetCountries = @("UNITED STATES", "CANADA")

# Process, filter, and save the data
Get-Content $sourceCsvPath | 
    Select-Object -Skip 1 | 
    ConvertFrom-Csv -Header "#Active Servers", "Street Address", "City", "Country" |
    Where-Object { $targetCountries -contains $_.Country } |
    Export-Csv -Path $filteredCsvPath -NoTypeInformation

Write-Host "Filtered data saved to: $filteredCsvPath"

Quick Tips for You

  • Dynamic Paths: Using [Environment]::GetFolderPath("Desktop") means your script works for any Windows user, no hardcoded usernames required.
  • -contains vs -eq: -contains is perfect for multi-value checks (matching any item in an array), while -eq only matches a single value (what you used before).
  • Export-Csv -NoTypeInformation: This removes the extra type metadata line PowerShell adds by default, keeping your CSV clean and easy to use in other tools.

内容的提问来源于stack exchange,提问作者Vishal Tarwani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 11:17:38