Hi,
I’ve been testing the following functions in order to create a housekeeping script for a filer:
function Remove-EmptyDirectories([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [switch] $DeletePathIfEmpty, [DateTime] $OnlyDeleteDirectoriesCreatedBeforeDate = [DateTime]::MaxValue, [DateTime] $OnlyDeleteDirectoriesNotModifiedAfterDate = [DateTime]::MaxValue, [switch] $OutputDeletedPaths, [switch] $WhatIf) { Get-ChildItem -Path $Path -Recurse -Force -Directory | Where-Object { (Get-ChildItem -Path $_.FullName -Recurse -Force -File) -eq $null } | Where-Object { $_.CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate -and $_.LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate } | ForEach-Object { if ($OutputDeletedPaths) { Write-Output $_.FullName } Remove-Item -Path $_.FullName -Force -Recurse -WhatIf:$WhatIf } # If we should delete the given path when it is empty, and it is a directory, and it is empty, and it meets the date requirements, then delete it. if ($DeletePathIfEmpty -and (Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -Force) -eq $null -and ((Get-Item $Path).CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate) -and ((Get-Item $Path).LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate)) { if ($OutputDeletedPaths) { Write-Output $Path } Remove-Item -Path $Path -Force -WhatIf:$WhatIf } } # Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind. function Remove-FilesCreatedBeforeDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory)][DateTime] $DateTime, [switch] $DeletePathIfEmpty, [switch] $OutputDeletedPaths, [switch] $WhatIf) { Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.CreationTime -lt $DateTime } | ForEach-Object { if ($OutputDeletedPaths) { Write-Output $_.FullName } Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf } Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty:$DeletePathIfEmpty -OnlyDeleteDirectoriesCreatedBeforeDate $DateTime -OutputDeletedPaths:$OutputDeletedPaths -WhatIf:$WhatIf }
This is courtesy of PowerShell Functions To Delete Old Files And Empty Directories - Daniel Schroeder’s Programming Blog
I have the path “C:\Octopus\Applications\New Folder” & “New Folder (2)”
My command is
Remove-FilesCreatedBeforeDate -Path “C:\Octopus\Applications” -DateTime ((Get-Date).AddDays(-0)) -Whatif -OutputDeletedPaths
Running the above command works fine and all folders under the “Applications” dir are checked and removed if empty but this is just a test environment and the actual hierarchy will have multiple folders under the “Applications” dir that I don’t want to be removed but too numerous to list as separate commands so want to use a wildcard as in “C:\Octopus\Applications*” but this only seems to run on “New Folder (2)”. I could comment out the call to the function “Remove-EmptyDirectories” but then this would stop this feature altogether which isn’t what I want.
Am I missing something to be able to use a wildcard as part of the path parameter?
Cheers