Powershell script show count of subfolders - powershell

I'm going around in circles trying to run a powershell script which returns the sub-folder count of sub-folders in my root directory.
E.g. Root directory C:\temp
Subfolders are C:\temp\1 and C:\temp\2 and each have x subfolders
I want to see the count of subfolders in C:\temp\1 and C:\temp\2 and if they are under C:\temp\1 or C:\temp\2.
So far I can only get a total subfolder count.

Pretty simple to do:
Get-ChildItem C:\Temp -Directory `
| Select-Object #{n='FullName';e={$_.FullName}},#{n='SubFolderCount';e={(Get-ChildItem $_.FullName -Recurse -Directory).Count}} `
| Format-Table -Autosize;

Related

Count all files in folder and subfolder + folders that says 0 files

I'm trying to run a Powershell command or CMD command, that count all files in 1 folder and its subfolder. I got a script that count all files, but it does not find hidden files. The problem I found was in my network share there was a folder that said 0 files, but when I go into the folder there are a lot of files in there.
Is there a way to get those files into the count?
I have tried a few Powershell command and CMD commands.
Get-ChildItem -Recurse -File | Measure-Object | %{$_.Count}
I can only get it to count files Windows can see, if a folder says 0 files but there are files in there, it does not count it.
Use the force
PS> Get-ChildItem -Path c:\test | Measure-Object | Select-Object -ExpandProperty Count
16
PS> Get-ChildItem -Path c:\test -Force | Measure-Object | Select-Object -ExpandProperty Count
17
The -Force parameter on Get-ChildItem will force inclusion of hidden files

Ignoring folder with large number of directories

I have written a PowerShell script that traverses over a directory and captures a list of folder names that are nested inside. It uses the following loop to achieve this
Get-ChildItem -Path $targetPath -Directory -Recurse |
Select-Object -ExpandProperty "Fullname" |
ForEach-Object {
#log folder name
}
However I want the script to skip directories that have a configured number of folders nested inside.
For example in the following example I do not want to capture the 'Folder C' because it has more than 3 folders nested.
Folder A/Folder AA
Folder A/Folder AB
Folder B/Folder BA
Folder C/Folder CA #DO NOT CAPTURE
Folder C/Folder CB #DO NOT CAPTURE
Folder C/Folder CC #DO NOT CAPTURE
Folder C/Folder CD #DO NOT CAPTURE
Folder D
So I would like my output of the script to be
Folder A/Folder AA
Folder A/Folder AB
Folder B/Folder BA
Folder D
How can I edit my script to achieve this?
If you just want to exclude folders on the top level you could do something like this:
Get-ChildItem -Path $targetPath -Directory | Where-Object {
(Get-ChildItem $_.FullName -Directory).Count -le 3
} | ForEach-Object {
Get-ChildItem $_.FullName -Directory -Recurse
} | Select-Object -Expand Fullname
If you want to exclude any (sub)folder that contains more than 3 subfolders you'll have to implement the recursion yourself. Get-ChildItem does not provide that kind of filtering mechanism.

How to find all folders which has folder named "Intro" AND filetype *.mp4

The closest I got was using powershell Get-ChildItem given multiple -Filters
Get-ChildItem -Include "Intro", "*.mp4" -Recurse
I think, -Include with multiple params work as OR operator. It gives folders with either "Intro" folder OR "*.mp4" files.
But I need AND condition. Folder must contain a folder named "Intro" and "*.mp4" files.
I need folders structured following -
E:.
└───test1.mp4
└───test2.mp4
└───test3.mp4
└───test4.mp4
└───test5.mp4
└───test6.mp4
└───Intro
Update 1
I am searching for folders which meet two condition.
It must have a subfolder named Intro AND
It must have *.mp4 files.
The answer would look something like the following I guess.
Get-ChildItem -Directory -Recurse {HasSubFolderNamedIntro && HasMP4Files}
Just wanted to add another Method (one-liner) to get the folder which contains mp4 files and a folder Intro (probs to #HenrikStanleyMortensen for most of it):
(Get-ChildItem -include "*.mp4" -Recurse -File).DirectoryName | Select-Object -Unique | Where-Object {(Get-ChildItem $_ -Recurse -Include 'Intro' -Directory)}
Do you need the command to only return the file objects or do you also want the folder objects?
If just the files I would do it like this:
Get-ChildItem -include "*.mp4" -Recurse -File | Where-Object {$_.Directory.Name -match 'Intro'}
So we use the include to find the mp4 files and reduce the amount of objects we pipe.
Then we pipe it to Where-Object and look for the property with the name of the folder and says we want it to contain the word "intro". If the folder needs to be called Intro exactly and not just contain it you can change the -match to -eq
Edit
To get the directories then we could do it like this:
(Get-ChildItem -include "*.mp4" -Recurse | Where-Object {$_.Directory.Name -match 'Intro'}).DirectoryName | Select-Object -Unique
Now we say that all the files that we found that matches our search, we want to see the full directory path of those files.
To only get one match per directory, so if we have 1 directory that matches with multiple mp4 files, and we don't want to see that same directory in our output one time per file, we can pipe the result into Select-Object -Uniqueto only see each directory once.
Edit 2
After clarification from OP.
To find a folder that contains both mp4 files and a subfolder called intro I don't think we can do that only from the Get-ChildItem command in any way I know of, so we can loop through each folder like this:
$Files = (Get-ChildItem -include "*.mp4" -Recurse -File).DirectoryName | Select-Object -Unique
foreach($File in $Files) {
Get-ChildItem -Path $File.DirectoryName -Recurse -Include 'Intro' -Directory
}
We Pipe to the Select-Object -Unique to make sure that folders with multiple mp4 files are not looped through more than once thus giving us an output with the same intro folder multiple times.

Scan out a folder + subfolder + get the files with a specific extension in PowerShell

I have a folder named LOGFILES with a subfolder named LOGFILES_OLD.
But we need to assume that I don't know the contents of the folder.
What I'm trying to develop in PowerShell is a method to show a list of all files with a .log extension.
I have failed to get it with something I have found here:
Get-Item C:\LOGFILES -Recurse | Where-Object {$_.Extension -eq ".log"} | Format-List
Why don't you use :
Get-ChildItem "C:\LOGFILES" -include *.log -Recurse

Powershell show recursive contents for specific folder names

I am trying to write a PowerShell script that will search for all folders named "abc" within a network share. There a multiple instances of this folder located throughout this share all named "abc" in different DIR's
I would like to list the file and folder contents, of every folder named "abc" within said directory. So far I have got PowerShell to list all instances of folders named "abc" within the network drive, but I am stuck after that.
Can I pipe this into another command that will then search and list each folders contents?
You can try something like htis
Get-ChildItem -Filter "abc" -Recurse -Path "\\myserver\share" | #Find abc
Where-Object { $_.PSIsContainer } | #Get only abc-folders
Get-ChildItem -Recurse | #Search through each abc-folder
Where-Object { !$_.PSIsContainer } #Get only files
The performance would be better if you run the script locally on the server or using psremoting(ex. using Invoke-Command). The problem is that it searches through all files and folders recursively before removing the files from the search result in that first Get-ChildItem line.
If you have PS 3.0 or PS 4.0, you can also use:
Get-ChildItem -Filter "abc" -Recurse -Path "\\myserver\share" -Directory | #Find abc-folders
Get-ChildItem -Recurse -File #Get files inside those folders