Solved

Issues with -exclude in powershell

Posted on 2013-01-21
11
406 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 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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 …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

679 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