Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

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
Avatar of ste5an
ste5an
Flag of Germany image

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?
Avatar of WeTi
WeTi

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi

ASKER

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