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
Solved

Issues with -exclude in powershell

Posted on 2013-01-21
11
405 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 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