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?

$ path = "D: \ apeda \ download" $ limit = (get-date) AddDays (-6) Go-ChildItem- $ path Path - Refinancing-force | Where-matter {! $ _ PSIsContainer- and $ _ CreationTime -lt $ border} | Remove-item-review-force

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

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -