Filip Heens
asked on
is there a way to automatically answer NO to All in a powershellscript
is there a way to automatically answer NO to All in a powershellscript
I made a script to automatically delete files older than 7 days and empty folders.
But sometimes I get the question "Confirm
The item at Microsoft.PowerShell.Core\ FileSystem ::xxxxxx has
children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
I want to automaticaaly answer NO to ALL on this... but I can't seem to find how to do this...
the script I use is...
I made a script to automatically delete files older than 7 days and empty folders.
But sometimes I get the question "Confirm
The item at Microsoft.PowerShell.Core\
children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
I want to automaticaaly answer NO to ALL on this... but I can't seem to find how to do this...
the script I use is...
$foldersfile = ".\folders.csv"
$folderpaths = Import-CSV $foldersfile -delimiter ";"
ForEach ($folderpath in $folderpaths){
$path=$folderpath."folderpath"
do{
Get-ChildItem -Path $Path -Filter 'thumbs.db' -Force -Recurse | remove-Item -force
$dirs = gci $path -directory -recurse | Where { (gci $_.fullName).count -eq 0 } | select -expandproperty FullName
$dirs | Foreach-Object { Remove-Item $_ }
#delete the thumbs.db files
write-host "folder done"
Get-ChildItem -Path $path -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-7))} | remove-Item
write-host "files done"
} while ($dirs.count -gt 0) #end doWhile
}#end foreach
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks oDdA also ;-)
But other than that, you should delete the files first, then the empty folders. There's no need to check the folder again and again for files.
Try it like this:
Open in new window