Powershell script to delete files older than x days, but not folders -
I have a script that I thought was working, but I am receiving reports that instead of files Can it be removed from folders that help me adjust my script in the account for it? Do I need to add a one-file filter?
You do not work the most where-object segment Just like you expect it
{! $ _ PSIsContainer- and $ _ CreationTime -lt $ border}
should be
{! $ _ PSIsContainer- and ($ _ CreationTime -lt $ border)}
Brackets ensure that the second logical comparison is independently assessed. For more information see about_logical_operators
The Windows PowerShell logical operators evaluate the only required statement to determine the true value of the statement. If the left operation in which there is an operator and is false in a statement, then the correct operand is not evaluated. If left or right statement in which statement or statement is correct, then the correct operand is not evaluated. As a result, you can use these statements in the same way that you will use I statement.
Since then it is removing files and not folders -Recurse
Remove-item
seems unnecessary for ALRC's comments From: Assume that you have version 3 or higher above PowerShell, you make it easy by using Get-ChildItem
using the -file
code >> ! $ _ PSIsContainer
section of your where-object
(which is usually done in version 2). As you have in your comment:
Go -Balitim -path $ path- file-protect-force | Where the object {$ _ CreationTime -lt $ limit}
Comments
Post a Comment