Solved

Issues with -exclude in powershell

Posted on 2013-01-21
11
408 Views
Last Modified: 2013-01-21
Hi guys, I've been trying to get a script working that will go through all the folders and files in a location and add them to a text file then delete them all excluding a couple of folders but I haven't got a lot of experience with powershell yet and I was looking for advice.

Here's the script so far:

$targetfolder = "C:\ScriptTest\ftproot"

$date = get-date -format ddMMyyyy

Get-ChildItem $targetfolder -Exclude "C:\ScriptTest\ftproot\google", "C:\ScriptTest\ftproot\Expedia", "C:\ScriptTest\ftproot\Corpedia" | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2)} | Out-File "C:\ScriptTest\ftproot\$Date.txt"

Get-ChildItem $targetfolder -Exclude "C:\ScriptTest\ftproot\google", "C:\ScriptTest\ftproot\Expedia", "C:\ScriptTest\ftproot\Corpedia" | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2)} | remove-item

Open in new window


I've run everything up till the delete part but the text file is still picking up the folders I want to exclude, can anyone offer advice?
0
Comment
Question by:thomasmulligan
[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
  • 5
  • 3
  • 3
11 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 38800911
Try this.

I have not tested but I think your quotes might be your issue.

$targetfolder = "C:\ScriptTest\ftproot"

$date = get-date -format ddMMyyyy

$excluded = "C:\ScriptTest\ftproot\google, C:\ScriptTest\ftproot\Expedia, C:\ScriptTest\ftproot\Corpedia"


Get-ChildItem $targetfolder -Exclude $excluded | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2)} | Out-File "C:\ScriptTest\ftproot\$Date.txt"

Get-ChildItem $targetfolder -Exclude $excluded | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2)} | remove-item
                                  

Open in new window

0
 

Author Comment

by:thomasmulligan
ID: 38800941
That's great and works to exclude items in the folders but I've just realised I left out the -recurse parameter in the main post, I added -recurse where I thought it should go and it still excludes what is in the folders but it doesn't exclude subfolders, will I have to add each subfolder to the $excluded variable or is there another way?
Here is where I added -recurse to your code:

$targetfolder = "C:\ScriptTest\ftproot"

$date = get-date -format ddMMyyyy

$excluded = "C:\ScriptTest\ftproot\google, C:\ScriptTest\ftproot\Expedia, C:\ScriptTest\ftproot\Corpedia"


Get-ChildItem $targetfolder -Exclude $excluded -Recurse | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2)} | Out-File "C:\ScriptTest\ftproot\$Date.txt"

Get-ChildItem $targetfolder -Exclude $excluded -Recurse | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2)} | remove-item

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 38801246
Try this..
$targetfolder = "C:\ScriptTest\ftproot\"
$date = get-date -format ddMMyyyy
$Dir = GCI -Path $targetfolder -Recurse | ? {$_.PSIsContainer -and `
$_.fullname -ne 'C:\ScriptTest\ftproot\google' -and `
$_.fullname -ne 'C:\ScriptTest\ftproot\Expedia' -and `
$_.fullname -ne 'C:\ScriptTest\ftproot\Corpedia' } | Select -ExpandProperty fullname

$Dir | Get-ChildItem -Recurse | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2)} | Out-File "C:\ScriptTest\ftproot\$Date.txt"

$Dir | Get-ChildItem -Recurse | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2)} | remove-item -WhatIf

Open in new window


PS : I have added -WhatIf for testing, you can remove -WhatIf to delete the files..
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 38801312
Forgot to add Recurse for excluded folders.. Check this updated script..
$targetfolder = "C:\ScriptTest\ftproot\"
$date = get-date -format ddMMyyyy
$Dir = GCI -Path $targetfolder -Recurse | ? {$_.PSIsContainer -and `
$_.fullname -notmatch 'C:\\ScriptTest\\ftproot\\google' -and `
$_.fullname -notmatch 'C:\\ScriptTest\\ftproot\\Expedia' -and `
$_.fullname -notmatch 'C:\\ScriptTest\\ftproot\\Corpedia'} | Select -ExpandProperty fullname

If ($Dir) {
$Dir | Get-ChildItem -Recurse | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2)} | Out-File "C:\ScriptTest\ftproot\$Date.txt"
$Dir | Get-ChildItem -Recurse | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2)} | remove-item -WhatIf
}

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 38801323
$targetfolder = "C:\ScriptTest\ftproot"

$date = get-date -format ddMMyyyy

$excluded = "C:\ScriptTest\ftproot\google, C:\ScriptTest\ftproot\Expedia, C:\ScriptTest\ftproot\Corpedia"
$exlist = $excluded.split(",")


 
foreach ($excludeditem in $exlist)
{

Get-ChildItem $targetfolder -Exclude $excluded -Recurse | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2) -and _.fullname -notlike "*$excludeditem*"} | Out-File "C:\ScriptTest\ftproot\$Date.txt"

Get-ChildItem $targetfolder -Exclude $excluded -Recurse | Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-2) -and _.fullname -notlike "*$excludeditem*"} | remove-item
                                            


}

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 38801329
Oops it looks like subsun beat me to it  :(  with less overhead too :)
0
 

Author Comment

by:thomasmulligan
ID: 38801344
<deleted>
0
 

Author Comment

by:thomasmulligan
ID: 38801387
Jumped the gun with my last comment, I hadn't noticed subsun's updated response. I'm afraid it will still exclude the top level folders but not the subfolders. Thanks for the effort though, would it make more sense if I just added the subfolders to the exclude list?
0
 

Author Comment

by:thomasmulligan
ID: 38801406
Actually after having another look at the results of the text file none of the excluded files are showing up in it which is right, but the -whatif said that the subfolders would still be removed? I hope this information helps
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38801444
Not sure why it is giving you that result, I can see both the commands (Line 9 and 10) are same except one pipe the output to Out-File and other to Remove-Item.. Could you please double check the script ans see if it is same as I posted in my comments?
0
 

Author Comment

by:thomasmulligan
ID: 38801504
That was my mistake sorry, rookie mistake I didn't cls before I ran the updated one and I scrolled too far. That seems to work excellently now thank you both for your help! I'll try it in the test environment now without the -whatif just to be sure but that should work thank you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
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…

751 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