Count directories with a certain name that also contain file - powershell

I am currently working on a script that will count all of the directories (within a specified path, \\servername\logs\failures) which have a naming convention of "P0*" or "MININT*" - but the folders named as "MININT*" also must have a subfolder that contains a specified log file. Also, it should only be counting folders created in the last 24 hours.
Here is my current code, it keeps returning a zero value and I am not sure why. I have been searching for hours and tried different methods using recurse, Test-Path, etc. to no avail.
$imagefailures = Get-ChildItem '\\servername\logs\failures' -Directory |
Where-Object {
($_.Name -like "P0*") -or
(($_.Name -like "MININT*") -and (Test-Path "\WinPE_TS_X_Win\SMSTSLog\Get-Name.log")) -and
($_.LastWriteTime -gt (Get-Date).AddHours(-24))
} | Measure-Object | select -ExpandProperty Count

Try this:
$imagefailures = Get-ChildItem '\\servername\logs\failures' -Directory |
Where-Object {
($_.Name -like "P0*") -or
(($_.Name -like "MININT*") -and (Test-Path "$($_.FullName)\WinPE_TS_X_Win\SMSTSLog\Get-Name.log")) -and
($_.LastWriteTime -gt (Get-Date).AddHours(-24))`
} | Measure-Object | select -ExpandProperty Count
The path you are testing will always just be "\WinPE_TS_X_Win\SMSTSLog\Get-Name.log" if you do not append it to the folder path you are iterating on.

Related

PowerShell greater than also includes item that is equal

I am writing a PowerShell script to sort the directory and return folders with the names that start with the number sequence that is greater than specified sequence, but -gt operator acts as a -ge operator.
Here is the code I'm running:
Get-ChildItem C:\Users\USER\Testing -recurse |
Where-Object { $_.PSIsContainer -ge $true -and $_.Name -gt "003" -and $_.Name -match '^\d+.*$' } |
Select-Object Name
The response I get is:
005-folder
003-folder
004-folder
There seems to be the similar but opposite pattern for -le and -lt operators. Both of them do NOT include the equal item. So when I run
Get-ChildItem C:\Users\USER\Testing -recurse |
Where-Object {$_.PSIsContainer -ge $true -and $_.Name -le "003" -and $_.Name -match '^\d+.*$' } |
Select-Object Name
The response I get is:
001-folder
002-folder
I can't seem to find anything on the internet that solves the issue I am having, so I assume something in my scripts breaks the -gt and -le operators?
I think I figured out the issue few minutes after I posted it. '003-folder' will be greater than '003' cause I'm comparing strings. Need to truncate first.
Get-ChildItem C:\Users\USER\Testing -recurse -Directory |
Where-Object {$_.Name.subString(0, 3) -gt '003' -and $_.Name -match '^\d+.*$' } |
Select-Object Name

how do I find all empty directories with a given name using Powershell?

I want to use Powershell to find all empty directories within a file structure that are called "Information & Specification". I thought I had cracked it when this line
dir -Directory -recurse | Where-Object {$_.Name -eq "Information & Specification" -and (Measure-Object).count -eq 0} | Select-Object -Property Parent
started returning results. However, it looks like it finds all "Information & Specification" folders irrespective of whether they have files in or not - if I reverse the count condition from -eq to -ne I get nothing.
So I set up a simple test hierachy with folders A, B, C at top level. A and B both contain a folder called X. (I am going to search for X rather than "Information & Specification" in my test). Folder C contains folder D, and D contains a folder called X.
I ran
dir -Directory -recurse | Where-Object {$_.Name -eq "X" -and (Measure-Object).count -eq 0} | Select-Object -Property Parent
and got
Parent
......
A
B
D
which appeared correct. But when I created a file in the B/X folder, I still got the same result, so clearly my test for emptiness is wrong. I referred back to Count items in a folder with PowerShell and tried
dir -Directory -recurse | Where-Object {$_.Name -eq "X" -and (Get-ChildItem $_ | Measure-Object).count -eq 0} | Select-Object -Property Parent
but this yields an error message I do not understand
Get-ChildItem : Cannot find path 'C:\temp\search_test\X' because it does not exist.
Can anyone help with the test for emptiness (or an entirely alternative solution?)
this uses the .GetFileSystemInfos() method of a directory to get any subdirs AND any files. the output is the list of directory full names. if you want the objects, remove the final .FullName. [grin]
$TopDir = 'D:\Temp'
$DirToFind = 'Sample'
$EmptyDirList = #(
Get-ChildItem -LiteralPath $TopDir -Directory -Recurse |
Where-Object {
#[System.IO.Directory]::GetFileSystemEntries($_.FullName).Count -eq 0
$_.GetFileSystemInfos().Count -eq 0 -and
$_.Name -match $DirToFind
}
).FullName
$EmptyDirList
truncated output ...
D:\Temp\zzz - Copy\Destination\DEcho\Music\Sample Music
D:\Temp\zzz - Copy\Destination\DEcho\Pictures\Sample Pictures
D:\Temp\zzz - Copy\Destination\DEcho\Videos\Sample Videos
[*...snip...*]
D:\Temp\zzz - Copy\Users - Copy\Admin\Music\Sample Music
D:\Temp\zzz - Copy\Users - Copy\Admin\Pictures\Sample Pictures
D:\Temp\zzz - Copy\Users - Copy\Admin\Videos\Sample Videos
all of those dirs are empty. [grin]
The easiest way to get a count is to assign the get-childitem to a variable then .count that variable. Such as:
$fileCount = get-childitem "C:\temp\search_test\X'
if ($fileCount.count -gt 0)
{
Write-Host "Files found!"
}
else
{
Write-Host "No files found."
}
This will give you the FullName of all empty directories under C:\Test:
Get-ChildItem -Path "C:\Test" -Directory -Recurse | Where-Object -FilterScript {($_.GetFiles().Count -eq 0) -and ($_.GetDirectories().Count -eq 0) -and $_.Name -eq "Information & Specification"} | Select-Object -ExpandProperty FullName
From here you can do handle the results anyway you want.

Open the first file in a list of recursed folders

I'm trying to open the most recent picture from all my folders in my onedrive using powershell. I have already gotten to the point where i have a "list" which automatically sorts to newest and only gives me the "first 1" result.
I've tried implementing "Select-Object ().Open" but i don't see where i need to implement it. I can't get it to work.
I've also tried taking the directory and filename in variables to try and paste these together to then use these to open the picture.
This is my command:
Get-ChildItem -Recurse | Where-Object {$_.Name -match "(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge "03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1
I'm expecting that there is a way to do this action and open this file in 1 command.
EDIT: I just discovered "Fullname" in the "Select-Object" function which is my directory and filename togheter. I still don't know how to implement ".Open" with this though.
Try this -
$FirstFile = Get-ChildItem -Recurse | Where-Object {$_.Name -match "(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge "03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1
Start-Process $FirstFile.FullName
You can also put the whole thing into a single line, though breaking the code will provide more readability -
Start-Process (Get-ChildItem -Recurse | Where-Object {$_.Name -match "`
(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge`
"03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1).FullName

How exploit Get-ChildItem result?

I would like list all path to go to folder name ending with "_S" recursively.
I did that:
Get-ChildItem -Recurse | Where-Object {($_.Attributes -match "Directory") -and ($_.Name.EndsWith("_S") -and ($_.PSIsContainer -eq 1))}
But the result isn't an array. How i can to exploit the results ?
My goal is to have something like that:
Myfolder\folder1\folder1_S
Myfolder\folder2_S
Use Select-Object and grab the FullName of the file.
Also, as stated in the comments on the question by #Paul ($_.Attributes -match "Directory") and ($_.PSIsContainer -eq 1) is redundant, might want to remove one of them.
Get-ChildItem -Recurse | Where-Object {($_.Attributes -match "Directory") -and ($_.Name.EndsWith("_S"))} | Select-Object -ExpandProperty FullName
The above can also be refactored in PowerShell 3.0+, to
Get-ChildItem -Recurse -Directory -Filter *_S | Select-Object -ExpandProperty FullName
which would recursively get the path of all directories ending with "_S"

Get parent directory and modification date from a file using Powershell

I'm looking into building a script that will get me both a date and parent directory of files created during a certain period.
So far this is what I've come up with:
get-childitem –recurse | where-object {($_.lastwritetime -gt “7/1/2013”) -and ($_.lastwritetime -le “7/22/2013”) }
I'm a bit clueless as to how to separate "Directory" and "LastWriteTime" (minus the time) into variables.
Would appreciate the help.
Thanks!
I wouldn't put them into separate variables. I'd just select the 2 properties:
$files = Get-ChildItem –Recurse | ? {
-not $_.PSIsContainer -and
$_.LastWriteTime -gt "7/1/2013" -and
$_.LastWriteTime -le "7/22/2013"
} | select Directory, #{n='LastWriteDate';e={Get-Date -uformat "%m\/%d\/%Y"}}
Then you can access those values like this:
$files[0].Directory.FullName
$files[0].LastWriteDate