+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
Coding Thread, PS - Remove-Item Exclude switch help in Coding and Web Development; Hi, I have a PS script I'm working on and need a little advice on excluding some folders. I have ...
  1. #1
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    604
    Thank Post
    68
    Thanked 17 Times in 16 Posts
    Rep Power
    20

    PS - Remove-Item Exclude switch help

    Hi,

    I have a PS script I'm working on and need a little advice on excluding some folders.

    I have the following code:
    Code:
    Remove-Item -recurse -path "$studentPath\My Settings\Application Data\*" -Exclude RM,Macromedia,Sun,"Internet*" -force
    The "Internet*" exclusion is to actually exclude the folder "Internet Explorer" inside the Microsoft folder however, this folder is retained but the content is still deleted. I was under the impression that this would exclude the folder and contents but obviously isn't the case.

    Any advice?
    Last edited by randle; 11th June 2014 at 11:31 AM.

  2. #2
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    604
    Thank Post
    68
    Thanked 17 Times in 16 Posts
    Rep Power
    20
    The recurse parameter appears to delete all content regardless of excluding the folders. Only the folders remain.

    Taking out the -recurse parameter does keep the contents of the first three folders but not the contents of the child folder "Internet Explorer" for some reason. The problem is that without this parameter it prompts for deletion...

  3. #3


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,774
    Thank Post
    262
    Thanked 2,963 Times in 2,178 Posts
    Rep Power
    838
    Try this...

    Code:
    $Exclusions = "RM","Macromedia","Sun","Internet*"
    
    Get-ChildItem -Path "$studentPath\My Settings\Application Data\" -Recurse -Exclude $Exclusions | Select -ExpandProperty FullName | Sort Length -Descending | Remove-Item -Recurse -Force -WhatIf
    Notes. Do not put an asterisk at the end of the path. Remove the '-WhatIf' parameter at the end to actually delete files and folders.

  4. #4


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,774
    Thank Post
    262
    Thanked 2,963 Times in 2,178 Posts
    Rep Power
    838
    Assuming you have PowerShell 3.0 (or greater) installed, you can also make the script run faster by adding the 'Directory' parameter to Get-ChildItem.

    Code:
    $Exclusions = "RM","Macromedia","Sun","Internet*"
    
    Get-ChildItem -Path "$studentPath\My Settings\Application Data\" -Directory -Recurse -Exclude $Exclusions | Select -ExpandProperty FullName | Sort Length -Descending | Remove-Item -Recurse -Force -WhatIf

  5. Thanks to Arthur from:

    randle (12th June 2014)

  6. #5
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    604
    Thank Post
    68
    Thanked 17 Times in 16 Posts
    Rep Power
    20
    Hi Arthur,

    Thanks for the help however, have the following outcome on a test area

    - The Macromedia and Sun folders are now empty. These had folders below them prior to running the script. I can't see why these are touched when the parent folders are set to be excluded!!?
    - The RM folder has retained a single file that existed so looks like files are now no longer being removed
    - The Microsoft folder which contained the Internet Explorer folder has been removed so the "Internet*" exclusion has been ignored by the looks of it.

  7. #6


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,774
    Thank Post
    262
    Thanked 2,963 Times in 2,178 Posts
    Rep Power
    838
    What happens if you remove the Recurse parameter from Remove-Item?

  8. Thanks to Arthur from:

    randle (12th June 2014)

  9. #7
    old_n07's Avatar
    Join Date
    Jun 2012
    Location
    North Staffordshire
    Posts
    99
    Thank Post
    10
    Thanked 17 Times in 15 Posts
    Rep Power
    8
    If you remove the recurse on the GCI part of the code it will only retrieve the top level folders excluding the items in exclusion list, then use recurse on the remove-item to delete the top level folders and their contents

    Code:
    $Exclusions = "RM","Macromedia","Sun","Internet*"
    
    Get-ChildItem -Path "$studentPath\My Settings\Application Data\" -Directory -Exclude $Exclusions | Select -ExpandProperty FullName | Sort Length -Descending | Remove-Item -Recurse -Force
    HTH

    Neil

  10. #8
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    604
    Thank Post
    68
    Thanked 17 Times in 16 Posts
    Rep Power
    20
    Now we're cooking. I've removed the recurse parameter from the gci section and now leaves the content and files o the excluded folders behind however, still isn't retaining the Internet Explorer folder below the Microsoft folder!

    Just seen your update Neil and confirms the above. Without the recurse parameter the IE folder is not kept. Is there any way around this?
    Last edited by randle; 12th June 2014 at 01:31 PM.

  11. #9
    old_n07's Avatar
    Join Date
    Jun 2012
    Location
    North Staffordshire
    Posts
    99
    Thank Post
    10
    Thanked 17 Times in 15 Posts
    Rep Power
    8
    Not sure on how to do it easily in one line but this will do it in two

    Code:
    $Exclusions = "RM","Macromedia","Sun","Microsoft"
    
    Get-ChildItem -Path "$studentPath\My Settings\Application Data\" -Directory -Exclude $Exclusions | Select -ExpandProperty FullName | Sort Length -Descending | Remove-Item -Recurse -Force
    Get-ChildItem -Path "$studentPath\My Settings\Application Data\Microsoft" -Directory -Exclude "Internet*" | Select -ExpandProperty FullName | Sort Length -Descending | Remove-Item -Recurse -Force
    Exclude the Microsoft directory as a whole from the first delete then perform the same function just on the MS folder excluding IE

    Neil

  12. Thanks to old_n07 from:

    randle (12th June 2014)

  13. #10
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    604
    Thank Post
    68
    Thanked 17 Times in 16 Posts
    Rep Power
    20
    Ah yes of course. I think that'll work fine for what I want. Thanks for that.

    Just out of interest, why does the gci command need to be piped through to "Select -ExpandProperty FullName | Sort Length -Descending". I can't see what this adds to help the process!?

  14. #11
    old_n07's Avatar
    Join Date
    Jun 2012
    Location
    North Staffordshire
    Posts
    99
    Thank Post
    10
    Thanked 17 Times in 15 Posts
    Rep Power
    8
    The "Select -ExpandProperty FullName " returns the full path and name of the folder and the sort just orders the output, but thy can both be omitted from the code and it still works.

  15. Thanks to old_n07 from:

    randle (12th June 2014)

  16. #12


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,774
    Thank Post
    262
    Thanked 2,963 Times in 2,178 Posts
    Rep Power
    838
    Quote Originally Posted by randle View Post
    Just out of interest, why does the gci command need to be piped through to "Select -ExpandProperty FullName | Sort Length -Descending". I can't see what this adds to help the process!?
    Because this...

    The -recurse switch does not work properly on Remove-Item (it will try to delete folders before all the child items in the folder have been deleted). Sorting the fullnames in descending order by length ensures that no folder is deleted before all the child items in the folder have been deleted. (Source)
    Last edited by Arthur; 12th June 2014 at 02:35 PM.

  17. 2 Thanks to Arthur:

    old_n07 (12th June 2014), randle (12th June 2014)

  18. #13
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    604
    Thank Post
    68
    Thanked 17 Times in 16 Posts
    Rep Power
    20
    Ah yes I remember seeing that now but was before using the gci Cmdlet so didn't pay too much attention to it. Thanks again guys

  19. #14
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    604
    Thank Post
    68
    Thanked 17 Times in 16 Posts
    Rep Power
    20
    Just one other question while I'm here if you wouldn't mind. I would think this an easy addition but am struggling to see how I can output all actions of the script to a file. I'm searching for a solution at the moment but from what I can see doesn't look to be as straight forward as I was hoping.....

  20. #15
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    604
    Thank Post
    68
    Thanked 17 Times in 16 Posts
    Rep Power
    20
    I've managed to have a bit of luck using the tee Cmdlet and all is now outputting to a file except one command. It's probably easier to post the script
    Code:
    # Server to run command on
    $serverName = Read-Host "Server Name" | tee "\\svroffice01\d$\Temp\Test\test.txt" -Append
    
    # Display directory size
    #Get-Size "\\$serverName\d$\Temp\Test\RMStudentWork"
    "Directory size pre cleanup: {0:N2}" -f ((Get-ChildItem -path "\\$serverName\d$\Temp\Test\RMStudentWork" -recurse | Measure-Object -property length -sum ).sum /1MB) + " MB" | tee "\\svroffice01\d$\Temp\Test\test.txt" -Append
    
    # Get a list of Intake years
    $intakeFolderPaths = gci "\\$serverName\d$\Temp\Test\RMStudentWork" -exclude *.ini
    
    # Cyle through users in each Intake year
    Foreach ($intakeYear in $IntakeFolderPaths) 
    {
    
    # Get a list of user names
    $studentFolderPath = gci $intakeYear -exclude *.ini
    
    # Assign path variable to each student in $studentFolderPath 
    foreach ($studentPath in $studentFolderPath)
    
    {
    # Remove all folders and files except exclusions within the My Settings folder
    $Exclusions = "RM","Macromedia","Sun","Microsoft","Adobe"
    
    Get-ChildItem -Path "$studentPath\My Settings\Application Data\" -Directory -Exclude $Exclusions | Select -ExpandProperty FullName | Sort Length -Descending | Remove-Item -Recurse -Force
    Get-ChildItem -Path "$studentPath\My Settings\Application Data\Adobe" -Directory -Exclude "Flash*" | Select -ExpandProperty FullName | Sort Length -Descending | Remove-Item -Recurse -Force
    Get-ChildItem -Path "$studentPath\My Settings\Application Data\Microsoft" -Directory -Exclude "Internet*" | Select -ExpandProperty FullName | Sort Length -Descending | Remove-Item -Recurse -Force
    
    # Confirm deletion to console, can be removed
    Write-Host "$studentPath My Settings cleaned up" | tee "\\svroffice01\d$\Temp\Test\test.txt" -Append
    }
    }
    
    # Display directory size
    #Get-Size "\\$serverName\d$\Temp\Test\RMStudentWork"
    "Directory size post cleanup: {0:N2}" -f ((Get-ChildItem -path "\\$serverName\d$\Temp\Test\RMStudentWork" -recurse | Measure-Object -property length -sum ).sum /1MB) + " MB" | tee "\\svroffice01\d$\Temp\Test\test.txt" -Append
    
    Pause
    The part that isn't outputting is the "Confirm deletion to console...." section. Am I adding this Cmdlet in the wrong place maybe?



SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Netgear Stack Switch Help
    By AIT in forum Wireless Networks
    Replies: 7
    Last Post: 16th July 2010, 10:43 AM
  2. Old Trick Remove Network Cable - Need Help Please!
    By james_richards in forum Windows
    Replies: 17
    Last Post: 14th February 2009, 04:06 PM
  3. MSI's Randomly not dpolying HP Procurve Switches HELP
    By noah in forum Wireless Networks
    Replies: 2
    Last Post: 12th September 2007, 03:37 PM
  4. Replies: 4
    Last Post: 2nd November 2006, 03:26 PM
  5. How Do I Remove Items From The System Tray.
    By tickmike in forum Windows
    Replies: 13
    Last Post: 6th September 2006, 11:32 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •