Hello:
how do i get unique file server names which has "ns" in their fqdn on multiple csv files in one folder, which has mulitple subfolders via powershell
thanks
Hello:
how do i get unique file server names which has "ns" in their fqdn on multiple csv files in one folder, which has mulitple subfolders via powershell
thanks
So you have tab separated values.
cls
$Servers = @()
Get-ChildItem "C:\temp\*.csv" -Recurse | foreach {
""
$_.FullName # display the file name that we are processing
$csv = Get-Content $_.FullName # read the file contents
foreach ($entry in $csv) {
$values = $entry.split("`t") # tab separated columns, split into an array
foreach ($v in $values) {
if ($v.contains(":\\")) { # drive letter : and unc path
$server = $v.split("\")[2] # get the server name
$server # display the name that we found
if ($server -match "-ns"){
$Servers += $server # add it to the results
}
}
}
}
}
""
"Here is what I found."
$Servers | Sort-Object -Unique
one step closer.. it also parse all file server names(duplicates as well).. is it possible to remove duplicates..
That's what the last line does. Note that there are only 3 entries in the "Here is what I found." section.

Assuming that each csv has a column named "server"...... and that the names are in the format "something.ns.somethingelse"...
cls
$Servers = @()
Get-ChildItem "C:\temp\*.csv" -Recurse | foreach {
$_.FullName # show the file name that we are processing.
$csv = Import-Csv $_.FullName
foreach ($entry in $csv) {
if ($entry.server -match "\.ns\."){ # use "ns" to look for just those 2 characters
$entry.server # show the server name that we found.
$Servers += $entry.server
}
}
}
""
"Here is what I found."
$Servers | Sort-Object -Unique
there is no column name "server"
I have no idea what contents are in any of your csv files. If you don't share details, then I can only take an educated guess and provide an example which you can then modify to suit your needs.
also name is in this format ab-ns01-cd..
Then just look for the 2 letters ns or dash ns.
if ($entry.server -match "ns"){
if ($entry.server -match "-ns"){
hi sorry for being vague. but those files have unc mapping to file servers.. so basically like this
usera P:\\ab-ns01-cd\groups\finance g:\\zd-ns10-de\public
so on and on
there is no column headers on those files. and i just need to collect unique file server names
so in this example i just need anything with ns i need parsed. i was going to upload .csv but it doesnt allow me import .csv so i converted into .txt
thanks
Hi there,
This example might help you with your requirements.
Get-ChildItem $PSScriptRoot\csv*.csv -File -PipelineVariable file | Import-Csv |
# Add new column "FileName" to distinguish the files.
Select-Object *, @{ label = 'FileName'; expression = { $file.Name } } |
# Group by ServiceCode to get a list of files per distinct value.
Group-Object ServiceCode |
# Filter by ServiceCode values that exist only in a single file.
# Sort-Object -Unique takes care of possible duplicates within a single file.
Where-Object { ( $_.Group.FileName | Sort-Object -Unique ).Count -eq 1 } |
# Expand the groups so we get the original object structure back.
ForEach-Object Group |
# Format-Table requires sorting by FileName, for -GroupBy.
Sort-Object FileName |
# Finally pretty-print the result.
Format-Table -Property ServiceCode, Foo -GroupBy FileName
--If the reply is helpful, please Upvote and Accept it as an answer–
19 people are following this question.