1

I've searched numerous MSDN/Technet and StackOverflow articles regarding this but I can't find a solution to my problem. SO references below.

I am trying to run a script on my server that simply counts the files in a folder on a network location.

I can get it working if it's a local folder, and I can get it working when I map the network drive. However I can't use a network drive because I'll be running this script from a web interface that doesn't have a user account (local drives work fine).

My script is:

$Files = Get-ChildItem \\storage\folder -File  
$Files.count

I get the error:

Get-ChildItem : Cannot find path '\\storage\folder' because it does not exist.

[0]open folder from Network with Powershell
[1]File counting with Powershell commands
[2]Count items in a folder with PowerShell
[3]Powershell - remote folder availability while counting files

Community
  • 1
  • 1
malnourish
  • 15
  • 1
  • 7

2 Answers2

0

Two things that I can think of,

One would be to add -path to your get-childitem call. I tested this on my Powershell and it works fine.

$files = get-childitem -path C:\temp $files.count This returns the number of files in that path.

However I am testing this on a local file. If you are sure it is the remote access part giving you trouble I would suggest trying to set credentials. Besides the get-credentials option, you could also try setting them yourself.

$Credentials = New-Object System.Management.Automation.PsCredential("Username", "password")

Then perhaps you can set the drive and still be able to access your files. Hope that helps.

Foster
  • 746
  • 2
  • 8
  • 14
0

Try this:

set-location \\\storage\folder\

dir -recurse |  where-object{ $_.PSIsContainer } | ForEach{ Write-Host $_.FullName (dir $_.FullName | Measure-Object).Count } 

This will count the number of files in each sub-folder (recurse) and display the full path and count in the output.

CodeChimp
  • 4,745
  • 6
  • 45
  • 61