Powershell one liner, measure the file count in server list.

Dear expert

Please help with the code below.
Get-Content -Path C:\Scripts\serverlist.txt | Where-Object {Get-ChildItem -File -Path $_} | ForEach-Object {"File in $_"}

Open in new window

This one liner shows: in serverlist.txt if there is file in it or not, now I would like add a count in each server list how many files in there. i tried with |measure| but I don't know how to use it, anyone can assist? I would see the output: File in \\servernamn\ and number after. Thanks
LVL 1
WeTiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
First of all: Don't post one-liners. They not readable. Use for developing them PowerShell ISE and you can place a line break after the pipe.

Then: How should that work? Should the script guess the share names?
WeTiAuthor Commented:
Hi thanks for reply, output result:

\\server1\folder    0
\\server2\folder    3

Just simple as that I changed abit in my code:

$server= Get-Content -Path C:\Scripts\serverlist.txt 
 
 ForEach($server in $servers) {
 gci -path $server | where {!$_.PSIsContainer} | Measure-Object
 "File in $server" }
 

Open in new window

Im still trying to figuer it out.
oBdACommented:
Verbose / Script version:
Get-Content -Path C:\Scripts\serverlist.txt | ForEach-Object {
	$count = (Get-ChildItem -File -Path $_ -ErrorAction SilentlyContinue | Measure-Object).Count
	If ($count -gt 0) {
		"$($count) Files in $($_)"
	}
}

Open in new window

Compressed / command line only (note the difference in readability):
gc C:\Scripts\serverlist.txt | % {$c = (gci $_ -File -ea si | measure).Count; If ($c -gt 0) {"$($c) Files in $($_)"}}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
Very fast indeed but maybe too fast, I change the code abit, thanks.

$servers = Get-Content -Path C:\Scripts\serverlist.txt 
ForEach ($server in $Servers) {
	$count = (Get-ChildItem -File -Path $server -ErrorAction SilentlyContinue | Measure-Object).Count
	If ($count -gt 0) {
		"$($count) Files in $($server)"
	}
}

Open in new window


Now works
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.