Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-16
10
Medium Priority
?
824 Views
Last Modified: 2014-01-27
- 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.
0
Comment
Question by:paulpp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39786548
$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
              }
          else
              {
#              Write-Host "No more folders to delete!" -foregroundcolor "Green"

            "No more folders to delete!" | add-content c:\result.txt
              }
          }
0
 
LVL 2

Author Comment

by:paulpp
ID: 39787097
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

0
 
LVL 19

Accepted Solution

by:
Raheman M. Abdul earned 2000 total points
ID: 39787119
Use the following instead

$folders = Get-Childitem $TargetFolder -Include *.view -filter *. -Recurse | where { $_.attributes -match "hidden"} | Where {$_.LastWriteTime -le "$LastWrite"}
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 71

Expert Comment

by:Qlemo
ID: 39787161
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.
0
 
LVL 2

Author Closing Comment

by:paulpp
ID: 39787174
Much appreciated Wizard!!!

:)
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39787183
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 ;-).
0
 
LVL 2

Author Comment

by:paulpp
ID: 39787190
LOL!!!
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39787891
I have to insist in knowing why you accepted a non-working suggestion.
0
 
LVL 2

Author Comment

by:paulpp
ID: 39788466
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.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39788499
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

0

Featured Post

Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question