I'm pretty new to PowerShell and have modified a script I found. It works like I expect it to locally on my windows computer (C:\ Drive). But when I run it over my network by changing the path to a network share on a NAS it lists all the directories correctly but all the file counts are zero.
I'm trying to find the file count in all directories and sub directories.
Here is the script:
Get-ChildItem $rootFolder -Recurse -Directory | Select-Object FullName, @{Name="FileCount";Expression={(Get-ChildItem $_ -File | Measure-Object).Count }}
C:\MyDirectory\Script = perfect
\\MyNetworkShare\MyDirectory\Script = proper directory listing but all file counts zero
What I'd ultimately like to do is find just directories with only a single (1) file in them. Can't do that till I get the NAS vs local thing figured out.
Thanks!
Added .FullName as suggested. Here is what the output looks like over network and locally. Can it be something with odd directories as I'm trying to manage a music library and lots of odd characters.
CodePudding user response:
Big thanks to @Santiago Squarzon! The solution was in fact to add -LiteralPath as noted in his comments. So the working script is:
Get-ChildItem $rootFolder -Recurse -Directory | Select-Object FullName, @{Name="FileCount";Expression={(Get-ChildItem -LiteralPath $_.FullName -File | Measure-Object).Count }}
And initially testing on local dive with "normal" directory names vs the very long odd ones for showing music release data must have been breaking things.
CodePudding user response:
It's not a one-liner but could prove useful?
Clear-Host
$rootFolder = '\\ComputerMentor2\CMShared\NAS-Downloads'
Get-ChildItem $rootFolder -Recurse -Directory |
Select-Object @{Name="FileCount";
Expression={([Array](Get-ChildItem -Path "$($_.Fullname)" -filter "*.*" -File)).count}},
@{Name="Path from Root";Expression={($_.FullName.Substring(($rootFolder.Length) 1))}} |
Sort -Property "Path from Root"
Sample Output:
FileCount Path from Root
--------- --------------
1 Calibre EBook Software
4 iPad Stuff
1 LibreOffice
1 No Longer Available Software
3 No Longer Available Software\Macrium
1 No Longer Available Software\MS Money
2 Peripheral Software
1 Peripheral Software\AMD
9 Purchased Software
1 SanDisk
43 Test
27 Useful-Utilities
1 Useful-Utilities\DG Readiness Tool
Useful-Utilities\DG Readiness Tool\dgreadiness_v3.6
6 Useful-Utilities\DG Readiness Tool\dgreadiness_v3.6\dgreadiness_v3.6
4 Useful-Utilities\Folder Marker Home
5 Useful-Utilities\Free File Sync
5 Useful-Utilities\Go Contact Sync Mod
9 Useful-Utilities\GRC
3 Useful-Utilities\irFanView
3 Useful-Utilities\MiniTool Partition Wizard
3 Useful-Utilities\NirSoft
4 Useful-Utilities\Samsung
1 Useful-Utilities\SARA
5 Video Codecs
5 Windows Kits
Note: Dir with no count has no files!