Answer the question
In order to leave comments, you need to log in
How to delete the necessary files/folders?
Hello PowerShell experts.
There was a question about deleting files on a remote machine.
The connection to it has already been implemented in Python, now I want to add the launch of PowerShell and the execution of the command to remove it.
Need help building a team.
Criteria:
$DT=New-Object DateTime
Get-ChildItem E:\Backup_Folder | Where {[DateTime]::TryParse($_,[ref]$DT)} | Where {[DateTime]::ParseExact($_,'yyyy-MM-dd',$null) -lt (Get-Date).AddDays(-60)}| Del -recurse -WhatIf
Answer the question
In order to leave comments, you need to log in
The author lies modestly.)
This script will only write that it will delete files older than 60 days from the current day.
But if you remove "-WhatIf" from the script, then it will delete it already ... If you have the rights to run unsigned scripts and have enough rights to delete files from the specified folder.
Wrote a simple universal function.
Function Del-Old{
Param(
#target path. Enter one string
$TargetPath,
#Enter one or many (array) exclude files (just names, not full address)
$ExcludePath = @(),
#Enter number of life time hours objects. Oldest objects will delete
[int]$TimeLifeHours
)
$TimeLimit = (Get-Date).AddHours(-$TimeLifeHours)
$ObjectsForDel = dir -Path $TargetPath -Exclude $ExcludePat | ?{$_.CreationTime -lt $TimeLimit}
foreach ($obj in $ObjectsForDel){
del -Path $obj.FullName -Recurse
}
}
the DelT program can easily handle your task.
Example:
delt e:\Example /PT /OF *.doc /NOD /RW /DTS 02/19/2019 /DTE 02/19/2019 /NOF *\folder\*
Delete all created/modified 02/19/2019 files with *.doc mask without transferring them to the recycle bin after overwriting their contents according to the 5220.22-M standard, in folders and subfolders, do not delete the folders themselves. Do not delete all files in folders.
The /RW key is most likely not needed in your case (overwriting before deleting).
If you want to first see what will be deleted without deleting itself, use the /T key.
If you want, see the /TM key with the number of hours and lifetime
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question