Hi,
When i try to write script on deleting files olderthan 30 days, it should capture log files (deleted files) please do help
Below is the script:
# Change the value $oldTime in order to set a limit for files to be deleted.
$oldTime = 30 # 30 day(s)
Cls
foreach ($path in Get-Content "D:\Suresh\paths.txt")
{
$Logfile = "D:\Files\log.txt"
Function LogWrite
{
Param ([string]$logstring)
Add-content $Logfile -value $logstring
}
# deleting the old files
Get-ChildItem $path -Recurse | WHERE-Object {($_.LastWriteTime -le $(Get-Date).AddDays(-$oldTime))} | Remove-Item -Force -Recurse
$Now = Get-Date
$Days = $oldTime
LogWrite "Trying to delete files older than $Days days, in the folder $path" -ForegroundColor Green
LogWrite "Start delete process at $Now"
$LastWrite = $Now.AddDays(-$Days)
LogWrite "LastWrite $LastWrite"
} out-file D:\Files\log.txt -Append
LogWrite "COMPLETE ALL" -ForegroundColor yellow
Not able to log deleted files in log file
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Not able to log deleted files in log file
Start with the following:
Most of what you have posted is just very bad guesswork. Here is a link that will get you up to speed.
• Learn PowerShell
• PowerShell Documentation
• PowerShell Style Guidelines
Code: Select all
Function LogWrite {
Param (
[string]$logstring,
$Logfile = 'D:\Files\log.txt'
)
Add-content $Logfile -value $logstring
}
$oldTime = 30 # 30 day(s)
$LastWrite = [datetime]::Today.AddDays(-$oldTime)
LogWrite "Trying to delete files older than $oldTime days, in the folder $path"
LogWrite "Start delete process at $Now"
LogWrite "LastWrite $LastWrite"
foreach ($path in (Get-Content D:\Suresh\paths.txt)){
Get-ChildItem $path -Recurse -File|
Where-Object {$_.LastWriteTime -le $LastWrite} |
Remove-Item -Force
}
LogWrite "COMPLETE ALL"
• Learn PowerShell
• PowerShell Documentation
• PowerShell Style Guidelines
Re: Not able to log deleted files in log file
Hi jvierra , thaks for your reply this script is not writing the file name in the logs(which was deleted)
Re: Not able to log deleted files in log file
You cannot get the filename when the delete is in a pipeline. This is why I posted the links. Start by learning how PowerShell and the pipeline work and how to design and write code that does what you need. Once you learn the basics you will be able to understand the issue and, perhaps, design the code you want.
Another way to delete files and get a complete log is to use RoboCopy which create a detailed log file.
Another way to delete files and get a complete log is to use RoboCopy which create a detailed log file.
Re: Not able to log deleted files in log file
Hi jvierra, I am new to power shell and started learning meanwhile can you please tell me how we can write deleted log name by using robocopy. Many thanks
Re: Not able to log deleted files in log file
Here is one easy way to get a list of files being deleted:
Code: Select all
$paths = Get-Content D:\Suresh\paths.txt
Get-ChildItem $paths -Recurse -File|
Where-Object {$_.LastWriteTime -le $LastWrite} |
Tee-Object -FilePath deleted.txt |
Remove-Item -Force -PassThru
Re: Not able to log deleted files in log file
RoboCopy is not PowerShell. It is a system utility. As with most system utilities you can get the help for the program by adding "/?" to the command line.
RoboCopy /?
RoboCopy /?
Re: Not able to log deleted files in log file
I tried the code which you given but my bad, not able to get the result, anyway thanks for your help
Re: Not able to log deleted files in log file
Hi jvierra,
I need some help on copying the script to remote servers and then execute the same copying script.
I know how to copy files from source to destination, but not able to execute the file script . please help me.
Below is the script.
$computers = get-content "C:\psscripts\servers.txt"
$source = "C:\test\script.ps1"
$destination = "C$\temp\"
foreach ($computer in $computers) {
if ((Test-Path -Path \\$computer\$destination))
{Copy-item $source -Destination "\\$computer\$destination" -Recurse}
else
{
"\\$computer\$destination is not reachable or does not exist"
}
}
Thanks in advance
I need some help on copying the script to remote servers and then execute the same copying script.
I know how to copy files from source to destination, but not able to execute the file script . please help me.
Below is the script.
$computers = get-content "C:\psscripts\servers.txt"
$source = "C:\test\script.ps1"
$destination = "C$\temp\"
foreach ($computer in $computers) {
if ((Test-Path -Path \\$computer\$destination))
{Copy-item $source -Destination "\\$computer\$destination" -Recurse}
else
{
"\\$computer\$destination is not reachable or does not exist"
}
}
Thanks in advance
Re: Not able to log deleted files in log file
How do you plan on executing the script? We do this in PowerShell using "Invoke-Command". It requires enabling and configuring PS remoting.
help enable-psremoting -online
help enable-psremoting -online