PowerShell - Find Wild Card Directory With Hidden Attribute Within Past X Days

- I need to create a PowerShell that will look for directories called "*.view".

- These directories have a Hidden attribute, so I only want to find those directories that are hidden.

- I'd like to look for directories that are older than X amount of days.

- For safety reasons, I'd like two options for output and I'll just comment out the one I don't need to use at any given time.

- First options is to create an output file to the desktop, so i can review it and make sure it's correct.

- The second option would be to actually remove the directories. Again, I'll just comment out the one I don't want to use when I first run the script.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
$Now = Get-Date
$Days = "7"
$TargetFolder = "C:\Temp\"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)

$folders = Get-Childitem $TargetFolder -Include *.view -filter *. -Attributes h  -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

# above $_.Lastwritetime can be replaced with $_.creationtime  to actually look for old days based on lastwritetime or creationtime.

foreach ($folder in $folders)
          if ($folder-ne $NULL)

#              write-host "Deleting folder $folder" -ForegroundColor "DarkRed"

              "Deleting folder $folder" | add-content c:\result.txt

#              Remove-Item $folder.FullName | out-null
#              Write-Host "No more folders to delete!" -foregroundcolor "Green"

            "No more folders to delete!" | add-content c:\result.txt
paulppAuthor Commented:
Hello Wizard,

I'm getting the following error message:

PS C:\Users\5sun5\Desktop> C:\Users\5sun5\Desktop\boo.ps1
Get-ChildItem : A parameter cannot be found that matches parameter name 'Attributes'.
At C:\Users\5sun5\Desktop\boo.ps1:7 char:78
+ $folders = Get-Childitem $TargetFolder -Include *.view -filter *. -Attributes <<<<  hidden  -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Open in new window

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Use the following instead

$folders = Get-Childitem $TargetFolder -Include *.view -filter *. -Recurse | where { $_.attributes -match "hidden"} | Where {$_.LastWriteTime -le "$LastWrite"}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Almost. Hidden folders and files are not shown by Get-ChildItem. And we really should restrict the search to folders (the above will also find files).
Further, first parsing the complete tree, and then act on the result is usually ineffective, so I recommend to use a pipe:
$Test= $true
$old = (get-date).AddDays(-7)     # 7 days back
Get-ChildItem -recursive -Filter *.view -Force | 
  ? { $_.Mode -like 'd??h' } |
  ? { $_.LastWriteTime -le $old } |
  % {
    if ($Test)
      Out-File C:\Results.txt "Deleting folder $($_.FullName)" 
    } else  {
        Write-Host write-host "Deleting folder $($_.FullName)" -ForegroundColor "DarkRed"
        Remove-Item -recursive -force $_

Open in new window

I took the liberty to define "to the desktop" as C:\ ;-).
Change $Test to $false to delete the folders.
paulppAuthor Commented:
Much appreciated Wizard!!!

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Did you test that script? It doesn't work.
And, btw, "Wizard" is a rank, not a name. The name is visible at the right of "by: ", and hence is "Qlemo" (for me) and "Raheman Mohammed Abdul" for the other Expert ;-).
paulppAuthor Commented:
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I have to insist in knowing why you accepted a non-working suggestion.
paulppAuthor Commented:
Qlemo, it 100% works. Why would you question my integrity on a message board? I have nothing to gain and all to lose if I were being dishonest.
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Because this does not show anything for me:
md x.view
attrib +h x.view
Get-Childitem -Include *.view -filter *. -Recurse 

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.