Assuming the input file is exactly as you have it display on screen I would do something like this:
$inputPath = "c:\temp\test.txt"
$outputPath = "C:\temp\results.txt"
$data = Get-Content $inputPath | Select -Skip 1 | ForEach-Object{
$splitLine = $_.split(' ',2)
[pscustomobject][ordered] @{
URL = $splitLine[0].Trim()
Environment = $splitLine[1].Trim()
}
}
$data | Group-Object URL | Select-Object @{Label="URL";Expression={$_.Name}},Count | ConvertTo-Csv -NoTypeInformation | Set-Content $outputPath
Write-Output "" | Add-Content $outputPath
Take the input file and for each line split it on the first space and return only two elements. Trim each split element to make a new object with parameters for URL and Environment.
Take that $data and feed it into Group-Object twice. Once for URL and once for Environment. Feed that data in a csv format to the $outputPath.
Sample Ouput (contents of $outputPath)
"URL","Count"
"127.0.0.1","2"
"127.0.0.2","2"
"127.0.0.4","1"
"Environment","Count"
"Prod","3"
"Test","2"
I'm sleepy so hopefully I read this one right.
[ordered]
If you only have PowerShell 2.0 this keyword wont exist. You can simply remove it from the code and it should work in place. Word of warning. [ordered] helps the output appear as coded Url then Environment. Using the Select statement account for that and organizes the output. If you added more fields don't forget to update the 2 select statements.
Group-Objectnot help in this situation? You just need to call it twice assuming you have formatted your input properly. Is that the issue?