Solved

Issues with -exclude in powershell

Posted on 2013-01-21
11
409 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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