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

WeTi
WeTi used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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?

Author

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.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial