Solved

Issues with -exclude in powershell

Posted on 2013-01-21
11
402 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
  • 5
  • 3
  • 3
11 Comments
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
$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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 28

Expert Comment

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

Author Comment

by:thomasmulligan
Comment Utility
<deleted>
0
 

Author Comment

by:thomasmulligan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
A procedure for exporting installed hotfix details of remote computers using powershell
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now