Recursively count filtered files in subfolders and group results - windows

I want to count all files with a particular prefix in a directory and then display the results based on each sub directory.
The directory tree is as follows
Test
January
sms20180101_110.unl
rec20180101_110.unl
data20180101_110.unl
February
sms20180201_110.unl
rec20180201_110.unl
data20180201_110.unl
March
sms20180301_110.unl
rec20180301_110.unl
data20180301_110.unl
So, I need to count for example the total data files in each subdirectory and display results as follows
January 1
February 5
March 10
I wrote the following command in Powershell
Get-ChildItem -Path D:\Test -Filter *data* -Force -Recurse | Measure-Object | %{$_.Count}
So, the problem is it is giving me the total files in the root directory
A similar question was asked here Recursively count files in subfolders but I have not been able to customize the solutions provided here to my need

Based on your scenario, you can use Group-Object like this -
Get-ChildItem -Path D:\Test -Filter *data* -Force -Recurse | Group-Object -Property Directory | Select-Object Name, Count
This will list all the name of the folders and sub-folders along with the count of files having data in it's name.

Related

PowerShell script to move files from one location to another. Only files which are more than 5 days old

How to create a powershell script that moves files from one location to another specifying that any file that is 5 days old does not move?
Used Move-Item to move files from one location to another, but I need to move files which are more than 5 days old.
I think this pipeline should works for you
Get-ChildItem -Path C:\Downloads -File | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-5)} | Move-Item -PipelineVariable $_ -Destination C:\button

Get folder permissions with only 3 levels of subfolders

First of all: sorry for my bad english.
So, I need to create various reports with all permissions of specified folders.
After some search I found 2 ways.
One is using AccessEnum, that it's almost perfect but it doesn't export all permissions, only the folders that have different permission from the root folder. And I need all of them, even if they are the same of the root folder.
The second one is better, a powershell script, but has one weakness: too much recursive, and one of the folders had an output report of 7GB. Holy shirt.
What I need: to modify the script to go deep only for 3 levels of subfolders, for example:
"C:\Folder1" contains various subfolders but I want the script to go deep only to "C:\Folder1\Folder2\Folder3\"
How can I do it?
This is the script:
dir -Recurse "C:\FOLDER" | where { $_.PsIsContainer } | % { $path1 = $_.fullname; Get-Acl $_.Fullname | % { $_.access | Add-Member -MemberType NoteProperty '.\Application Data' -Value $path1 -passthru }} | Export-Csv "C:\REPORT.csv"
Use
Get-Childitem
instead. It has a Depth-Parameter and you can only include Folders.
Reference:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7

Using PowerShell, identify the last TWO most recently created files in a single directory, compare them, and spit out only the differences to a file

I need some assistance on my attempt at my 3rd PowerShell script. I have a report being generated daily. It puts the information I need into a txt file in a Reports folder on my desktop. I'm needing a way to compare the most recent LAST report with the newest and then output the differences to a different file for review at a later date. This script will run as a .bat on every computer getting these reports (obviously only running if the computer is turned on so the dates will not always be back to back).
There will only be a single write to this directory every 24 hours which will be the daily generated report. The daily report file is named with the computer name in file name aa111111-report-currentdate.txt. If the 'Report' directory contains 2 or more files, run a compare on the most recently created file and the previously created file then spit out the differences to a new file in a different directory to identify new changes manually. Here is what I have:
if ((Get-ChildItem C:\users\x\Desktop\Report -File | Measure-Object).count -ge 2) {
Compare-Object (Get-ChildItem 'C:\users\x\Desktop\Report' | sort LastWriteTime | select -Last 1) -DifferenceObject (Get-ChildItem 'C:\users\x\Desktop\Report' | sort LastWriteTime | select -Last 2) -PassThru | Where-Object {$_.Name -match '^[a-zA-Z]{2}\d{6}[-]report[-]\d{4}[-]\d{2}[-]\d{2}\.txt$' -and $_.SideIndicator -eq "=>"} | Out-File -FilePath 'C:\users\x\Desktop\Report_Differences\differences.txt' -Width 9999999
}
(Get-ChildItem 'C:\users\x\Desktop\Report' | sort LastWriteTime | select -Last 1) looks like this:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 5/23/2020 8:45 PM 670146 DESKTOP-aa111111-report-2020-05-23.txt
(Get-ChildItem 'C:\users\x\Desktop\Report' | sort LastWriteTime | select -Last 2) looks like this:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 5/23/2020 8:45 PM 670146 DESKTOP-aa111111-report-2020-05-23.txt
-a---- 5/24/2020 9:45 PM 793676 DESKTOP-aa222222-report-2020-05-24.txt
Problem 1:
My current solution seems to identify both files as a single query and I'm not sure how to have PowerShell understand I need them identified separately to do a diff. How can I specify a compare on only the 2 most recently created files in the directory, diff them, then spit out results to another file in a separate directory WITHOUT knowing the exact file name (only the regex pattern which will match).
Extra: Is there a method to get the difference file to be formatted such as 'date of last file vs date of today' so I know which files/dates found the differences?
UPDATE
$last2createdfiles = Get-ChildItem 'C:\users\x\Desktop\Report' | sort LastWriteTime | select -Last 2
However, the below PowerShell does not output the file differences to a file. It only shows the output of the file name properties of $last2createdfiles[0] such as mode, lastwritetime,length, and name.
if ((Get-ChildItem C:\users\x\Desktop\Report -File | Measure-Object).count -ge 2) {
Compare-Object -ReferenceObject $last2createdfiles[0] -DifferenceObject $last2createdfiles[1] -PassThru | Where-Object {$_.SideIndicator -eq "=>"} | Out-File -FilePath 'C:\users\x\Desktop\Report_Differences\differences.txt'
}
What am I missing?

Script to list owners of sub-directories on a windows share

All I need is a list of sub-directories, given a starting directory, listing the owner of that directory.
Any ideas on easy way to do this?
Use the Get-ChildItem cmdlet to retrieve the sub-directories and determine the owner using the Get-Acl cmdlet:
Get-ChildItem '\\server\yourshare' |
where PSIsContainer |
select FullName, #{e={Get-Acl $_.FullName | select -expand Owner}; l="Owner"}

Why folder size is different from result of PowerShell?

I am running PowerShell code to check a folder size. I just noticed that the result is different from what I see in Windows GUI of folder properties.
part of PowerShell code:
#{label="Size" ; expression={(Get-childitem "c:\windows" -recurse | measure-object length -sum).sum}}
Results are not the same....?
BTW, how to display the size in GB based on the code above?
Hint please!
The reason you get different results is hidden files and folders. To account for these use -Force switch on Get-childitem:
#{label="Size" ; expression={(Get-childitem -force "c:\windows" -recurse | measure-object length -sum).sum}}
To get your total into GBs just divide the result by 1Gb, ie:
#{label="Size" ; expression={(Get-childitem -force "c:\windows" -recurse | measure-object length -sum).sum/1Gb}}
Or to round it up to one decimal point:
#{label="Size" ; expression={[math]::round(((Get-childitem -force "c:\windows" -recurse | measure-object length -sum).sum/1Gb),1)}}

Resources