I've been looking over this script for several days. Worked through a majority of the errors but can't seem to catch what is causing this error. The script is:
. $Env:JobDir\Scripts\DeleteMethods1.ps1
#Purge old logs
Remove-FilesCreatedBeforeDate -Path "$Env:JobDir\Logs\" -DateTime ((Get- Date).AddDays(-30))
#Backup log from last run
if(Test-Path $Env:JobDir\log_*.txt)
{
Move-Item -force $Env:JobDir\log_*.txt $Env:JobDir\Logs\
}
$logFile = $Env:JobDir + "\log_$(Get-Date -Format ""yyyyMMdd-HHmm"").txt"
echo "Removing files older than $Env:Days days" >> $logFile
if($Env:CheckType -eq "Created")
{
# Remove all files created more than 30 days ago
# Optionally use -DeletePathIfEmpty to remove empty directories
Remove-FilesCreatedBeforeDate -Path $Env:PathToCleanUp -DateTime ((Get- Date).AddDays($Env:Days)) -DeletePathIfEmpty "$Env:DeletePathIfEmpty" >> $logFile
}
if($Env:CheckType -eq "Modified")
{
# Delete all files that have not been updated in 30 days
Remove-FilesNotModifiedAfterDate -Path $Env:PathToCleanUp -DateTime ((Get- Date).AddDays($Env:Days)) "$Env:DeletePathIfEmpty" >> $logFile
}
if($Env:CheckType -eq "Accessed")
{
# Delete all files that have not been accessed in 30 days
Remove-FilesNotAccessedAfterDate -Path $Env:PathToCleanUp -DateTime ((Get-Date).AddDays($Env:Days)) "$Env:DeletePathIfEmpty" >> $logFile
}
echo "Done removing files older than $Env:Days days" >> $logFile
cat "$Env:JobDir\log.txt" >> $logFile
The script pulls functions from the following file:
function Remove-EmptyDirectories([parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string] $Path, [switch] $DeletePathIfEmpty,
[DateTime] $OnlyDeleteDirectoriesCreatedBeforeDate = [DateTime]::MaxValue,
[DateTime] $OnlyDeleteDirectoriesNotModifiedAfterDate = [DateTime]::MaxValue,
[DateTime] $OnlyDeleteDirectoriesNotAccessedAfterDate = [DateTime]::MaxValue)
{
Get-ChildItem -Path $Path -Recurse -Force -Directory | Where-Object { (Get- ChildItem -Path $_.FullName -Recurse -Force -File) -eq $null } |
Where-Object { $_.CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate -and $_.LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate
-and $_.LastAccessedTime -lt $OnlyDeleteDirectoriesNotAccessedAfterDate } |
Remove-Item -Force -Recurse
# If we should delete the given path when it is empty, and it is a directory, and it is empty, and it meets the date requirements, then delete it.
if ($DeletePathIfEmpty -and (Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -Force) -eq $null -and
((Get-Item $Path).CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate) -and
((Get-Item $Path).LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate) -and
((Get-Item $Path).LastWriteTime -lt $OnlyDeleteDirectoriesNotAccessedAfterDate))
{ Remove-Item -Path $Path -Force }
}
# Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind.
function Remove-FilesCreatedBeforeDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path,[parameter(Mandatory)][DateTime] $DateTime,[switch] $DeletePathIfEmpty)
{
Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.CreationTime -lt $DateTime } | Remove-Item -Force
Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty $DeletePathIfEmpty -OnlyDeleteDirectoriesCreatedBeforeDate $DateTime -OnlyDeleteDirectoriesNotModifiedAfterDate $DateTime -OnlyDeleteDirectoriesNotAccessedAfterDate $DateTime
}
# Function to remove all files in the given Path that have not been modified after the given date, as well as any empty directories that may be left behind.
function Remove-FilesNotModifiedAfterDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory)][DateTime] $OnlyDeleteDirectoriesNotModifiedAfterDate, [switch] $DeletePathIfEmpty)
{
Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate } | Remove-Item -Force
Remove-EmptyDirectories -Path $Path -OnlyDeleteDirectoriesNotModifiedAfterDate $OnlyDeleteDirectoriesNotModifiedAfterDate -DeletePathIfEmpty $DeletePathIfEmpty
}
# Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind.
function Remove-FilesNotAccessedAfterDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory)][DateTime] $OnlyDeleteDirectoriesNotAccessedAfterDate, [switch] $DeletePathIfEmpty)
{
Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.LastAccessTime -lt $OnlyDeleteDirectoriesNotAccessedAfterDate } | Remove-Item -Force
Remove-EmptyDirectories -Path $Path -OnlyDeleteDirectoriesNotAccessedAfterDate $OnlyDeleteDirectoriesNotAccessedAfterDate -DeletePathIfEmpty $DeletePathIfEmpty
}
Now the object of this script is to crawl through directories and delete old items. A lot of the variables in here are from environmentally set items. Now when I run this I get an error that states:
Remove-FilesCreatedBeforeDate : A positional parameter cannot be found that
accepts argument ''.
+ Remove-FilesCreatedBeforeDate -Path $Env:PathToCleanUp -DateTime
((Get-Date) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-FilesCreatedBeforeD
ate], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Remove-FilesCreatedB
eforeDate
I figure that I've been looking at this too long and I can't see the error so if it's something simple be kind. Anything as far as advice would be fantastic. Thanks in advance!!!
*Note I am not the one who wrote the script - a co-worker has me on error correction duty.
It has to be a problem with "$Env:DeletePathIfEmpty", since that's the only positional parameter you use in those statements. Based on the other places you call that function I'm guessing you need to change this:
"$Env:DeletePathIfEmpty"
to this:
-DeletePathIfEmpty "$Env:DeletePathIfEmpty"
It looks like that change needs to be made for both the "Modified" and "Accessed" checktype.
Based on your comment pointing out that the -DeletePathIfEmpty parameter is a [switch] it looks like you either need to just call out the -DeletePathIfEmpty switch or modify the function to a [bool] type and pass it either a $true or $false.
Related
this is my first post on this forum. Im a beginner in coding and I need help with one of my very first self coded tools.
I made a small script, which deletes files based on if they are older than date x (lastwritetime). Now to my problem: I want the script also to check for files inside of folders inside of a directory and only delete a folder afterwards if it is truly empty. I cant figure out how to solve the recursion in this problem, seems like the script deletes just the entire folder in relation to the date x. Could anyone tell me please what I missed in this code and help me to create a own recursion to solve the problem or fix the code? Thanks to you all, guys! Here is my code:
I would be glad if someone knows how to make the code work by using a function
$path = Read-Host "please enter your path"
"
"
$timedel = Read-Host "Enter days in the past (e.g -12)"
$dateedit = (Get-Date).AddDays($timedel)
"
"
Get-ChildItem $path -File -Recurse | foreach{ if ($_.LastWriteTime -and !$_.LastAccessTimeUtc -le $dateedit) {
Write-Output "older as $timedel days: ($_)" } }
"
"
pause
Get-ChildItem -Path $path -Force -Recurse | Where-Object { $_.PsisContainer -and $_.LastWriteTime -le $dateedit } | Remove-Item -Force -Recurse
""
Write-Output "Files deleted"
param(
[IO.DirectoryInfo]$targetTolder = "d:\tmp",
[DateTime]$dateTimeX = "2020-11-15 00:00:00"
)
Get-ChildItem $targetTolder -Directory -Recurse | Sort-Object {$_.FullName} -Descending | ForEach-Object {
Get-ChildItem $_ -File | Where-Object {$_.LastWriteTime -lt $dateTimeX} | Remove-Item -Force
if ((Get-ChildItem $_).Count -eq 0){Remove-Item $_ -Force}
}
remove -WhatIf after test
To also remove folders that are older than the set days in the past if they are empty leaves you with the problem that as soon as a file is removed from such a folder, the LastWriteTime of the folder is set to that moment in time.
This means you should get a list of older folders first, before you start deleting older files and use that list afterwards to also remove these folders if they are empty.
Also, a minimal check on user input from Read-Host should be done. (i.e. the path must exist and the number of days must be convertable to an integer number. For the latter I chose to simply cast it to [int] because if that fails, the code would generate an execption anyway.
Try something like
$path = Read-Host "please enter your path"
# test the user input
if (-not (Test-Path -Path $path -PathType Container)) {
Write-Error "The path $path does not exist!"
}
else {
$timedel = Read-Host "Enter days in the past (e.g -12)"
# convert to int and make sure it is a negative value
$timedel = -[Math]::Abs([int]$timedel)
$dateedit = (Get-Date).AddDays($timedel).Date # .Date sets this date to midnight (00:00:00)
# get a list of all folders (FullNames only)that have a LastWriteTime older than the set date.
# we check this list later to see if any of the folders are empty and if so, delete them.
$folders = (Get-ChildItem -Path $path -Directory -Recurse | Where-Object { $_.LastWriteTime -le $dateedit }).FullName
# get a list of files to remove
Get-ChildItem -Path $path -File -Recurse | Where-Object { $_.LastWriteTime -le $dateedit} | ForEach-Object {
Write-Host "older as $timedel days: $($_.FullName)"
$_ | Remove-Item -Force -WhatIf # see below about the -WhatIf safety switch
}
# now that old files are gone, test the folder list we got earlier and remove any if empty
$folders | ForEach-Object {
if ((Get-ChildItem -Path $_ -Force).Count -eq 0) {
Write-Host "Deleting empty folder: $_"
$_ | Remove-Item -Force -WhatIf # see below about the -WhatIf safety switch
}
}
Write-Host "All Done!" -ForegroundColor Green
}
The -WhatIf switch used on Remove-Item is there for your own safety. With that, no file or folder is actually deleted, instead in the console it is written what would be deleted. If you are satisfied that this is all good, remove the -WhatIf and run the code again to really delete the files and folders
try something like this:
$timedel=-12
#remove old files
Get-ChildItem "C:\temp" -Recurse -File | Where LastWriteTime -lt (Get-Date).AddDays($timedel) | Remove-Item -Force
#remove directory without file
Get-ChildItem "C:\temp\" -Recurse -Directory | where {(Get-ChildItem $_.FullName -Recurse -File).count -eq 0} | Remove-Item -Force -recurse
Hi this is the first time I create a program using powershell, I created a powershell script to move old files that are not in use to a NAS, the code works as what I want, but I need a Log.txt file for find out what files have been moved. can someone please help me?
$Date = Get-Date -UFormat %d-%m-%Y
$Source = 'C:\Source\'
$Temp = 'C:\Backup-Temp\'
$Nas = 'D:\Destination\'
$Ext = "*.zip","*.rar"
$SetTime = '-5'
New-Item -Path $Temp -Name "Backup-$Date" -ItemType "directory"
Foreach ($Ext in $Ext) {
get-childitem -Path "$Source" -Recurse |
Where-object {$_.LastWriteTime -lt (get-date).AddDays($SetTime) -and $_.name -like "$Ext"} |
Move-item -destination "$Temp\Backup-$Date" |
Compress-Archive -Path "$Temp\Backup-$Date" -DestinationPath "$Nas\Backup-$date.Zip"
}
$Date = Get-Date -UFormat %d-%m-%Y
$Source = 'C:\Source\'
$Temp = 'C:\Backup-Temp\'
$Nas = 'D:\Destination\'
$Ext = "*.zip","*.rar"
$SetTime = '-5'
$LogFileFullName = 'c:\tmp\log.txt'
function Write-Log([string]$msg){
Out-File -FilePath $LogFileFullName -InputObject "$([DateTime]::Now): $msg" -Append
}
New-Item -Path $Temp -Name "Backup-$Date" -ItemType "directory"
Foreach ($Ext in $Ext) {
get-childitem -Path "$Source" -Recurse |
Where-object {$_.LastWriteTime -lt (get-date).AddDays($SetTime) -and $_.name -like "$Ext"} |
ForEach-Object {
Move-item $_.FullName -destination "$Temp\Backup-$Date" |
Compress-Archive -Path "$Temp\Backup-$Date" -DestinationPath "$Nas\Backup-$date.Zip"
Write-Log $_.FullName
}
}
You could just add the following to your chain of piped commands:
Add-Content $logfile "$_.name`n"
where $logfile is set to a static filename prior.
I may be old-fashioned, but having so many commands in one chain is prone to failure. It would be more resilient to break-up the chain so that you can include some error checking along the way.
A better but less desirable option would be to put your chain within a try/catch block.
Best of luck.
I am trying to find the path of the object currently being operated on in a for-eachobject loop. Can someone explain how to do this? I am trying to set the path of the source for Copy-Item to whatever path the file is on. This is the first project I have worked with Powershell on. Assume $Src has been properly instantiated.
Get-ChildItem -Path $Src -Recurse -Force | Where-Object {$_.Name -notmatch "Archive" -and ($_.PSIsContainer)}|
ForEach-Object {
$DateStr = $_.BaseName.Substring(0,2)+'/'+$_.BaseName.Substring(3,2)+'/'+$_.BaseName.Substring(6,4)
$FileDate = get-date $DateStr
If ( $FileDate -ge $Date -and !($_.PSIsContainer)) {
$ParentOjbect = $_.Directory
$Parent = $ParentOjbect.Name
Copy-Item -Path (Join-Path -Path $Src -ChildPath '\*.txt' ) #this
#needs to be corrected to only point to the current file
-Dest (Join-Path $Dst ("$($_.Directory.Name) $($_.Name)")) -WhatIf
}
}
Copy-Item -Path (Join-Path -Path $_.FullName -ChildPath '\*.txt' )
I suggest you also use -WhatIf on that also to check it does what you expect first.
$_, or $PSItem (PowerShell 3.0+), accesses the current object in the pipeline. From here, you can access its members.
I'm fairly new to Powershell but manged so far to pull together a script that deletes files older than a defined Created Date and excluding certain file types. However, I'm struggling to incorporate both verbose and file logging output. I've tried various methods I've found online and I think Out-File is the most appropriate however I simply can't make it work. Hoping someone can help!
Set-StrictMode -Version Latest
# $Logfile = "C:\Temp\Log.log"
function Remove-Files([parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory=$true)][DateTime] $DateTime, [switch] $WhatIf)
{
Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $DateTime -and ($_.Name -notlike "*.txt"-and $_.Name -notlike "*.log")} |
# Out-File -filepath $logfile -append
ForEach-Object { Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf}
}
Remove-Files -Path "C:\Temp" -DateTime ((Get-Date).AddDays(-10)) # -whatif
You are not sending any content to the log file.
Uncomment the $logfile declaration, and use this for instance:
ForEach-Object {
Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf
"Removed $($_.FullName)" | Out-File -FilePath $logfile -Append
}
What you want to do is just Tee-Object the files before you remove them. Like, literally just replace your Out-File with Tee-Object:
function Remove-Files {
[CmdletBinding()]
param(
[parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string]$Path,
[parameter(Mandatory=$true)]
[DateTime]$DateTime,
# Personally, I would pass the log file as a parameter
# [string]$LogFile,
[switch]$WhatIf
)
Get-ChildItem -Path $Path -Recurse -Force |
Where-Object {
!$_.PSIsContainer -and
$_.CreationTime -lt $DateTime -and
($_.lName -notlike "*.txt" -and $_.Name -notlike "*.log")
} |
Tee-Object -Filepath $LogFile -Append |
Remove-Item -Force -WhatIf:$WhatIf
}
Remove-Files -Path "C:\Temp" -Log "C:\Temp\Rm.log" -DateTime ((Get-Date).AddDays(-10))
The only problem is that:
The output in the log would be formatted the same as if you output to the console, so it's not quite what you'd normally log ...
The log will be the same whether you remove or not (i.e.: -Whatif makes it not delete, but doesn't stop the log)
Here's my updated code to get the logging working..
function Remove-FilesCreatedBeforeDate([parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory=$true)][DateTime] $DateTime, [string]$LogFile = "C:\Temp\Log.log", [switch] $WhatIf)
{
"LOG START $(Get-Date –f "yyyy-MM-dd HH:mm:ss")" | Out-File -FilePath $logfile -Append
Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $DateTime -and ($_.Name -notlike "*.txt"-and $_.Name -notlike "*.log")} |
ForEach-Object { Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf
"$(Get-Date –f o) Removed $($_.FullName)" | Out-File -FilePath $logfile -Append | Write-host "Removed $($_.FullName)"}
"LOG END $(Get-Date –f "yyyy-MM-dd HH:mm:ss")" | Out-File -FilePath $logfile -Append
}
Remove-FilesCreatedBeforeDate -Path "C:\Temp" -DateTime ((Get-Date).AddDays(-0))
A while ago I have created a Log-Entry framework where you can do inline logging like:
Remove-Item -Path (Log "Removing:" $_.FullName ?) -Force -WhatIf:$WhatIf
I would like to delete only the files that were created more than 15 days ago in a particular folder. How could I do this using PowerShell?
The given answers will only delete files (which admittedly is what is in the title of this post), but here's some code that will first delete all of the files older than 15 days, and then recursively delete any empty directories that may have been left behind. My code also uses the -Force option to delete hidden and read-only files as well. Also, I chose to not use aliases as the OP is new to PowerShell and may not understand what gci, ?, %, etc. are.
$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"
# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
And of course if you want to see what files/folders will be deleted before actually deleting them, you can just add the -WhatIf switch to the Remove-Item cmdlet call at the end of both lines.
If you only want to delete files that haven't been updated in 15 days, vs. created 15 days ago, then you can use $_.LastWriteTime instead of $_.CreationTime.
The code shown here is PowerShell v2.0 compatible, but I also show this code and the faster PowerShell v3.0 code as handy reusable functions on my blog.
just simply (PowerShell V5)
Get-ChildItem "C:\temp" -Recurse -File | Where CreationTime -lt (Get-Date).AddDays(-15) | Remove-Item -Force
Another way is to subtract 15 days from the current date and compare CreationTime against that value:
$root = 'C:\root\folder'
$limit = (Get-Date).AddDays(-15)
Get-ChildItem $root -Recurse | ? {
-not $_.PSIsContainer -and $_.CreationTime -lt $limit
} | Remove-Item
Basically, you iterate over files under the given path, subtract the CreationTime of each file found from the current time, and compare against the Days property of the result. The -WhatIf switch will tell you what will happen without actually deleting the files (which files will be deleted), remove the switch to actually delete the files:
$old = 15
$now = Get-Date
Get-ChildItem $path -Recurse |
Where-Object {-not $_.PSIsContainer -and $now.Subtract($_.CreationTime).Days -gt $old } |
Remove-Item -WhatIf
Try this:
dir C:\PURGE -recurse |
where { ((get-date)-$_.creationTime).days -gt 15 } |
remove-item -force
Esperento57's script doesn't work in older PowerShell versions. This example does:
Get-ChildItem -Path "C:\temp" -Recurse -force -ErrorAction SilentlyContinue | where {($_.LastwriteTime -lt (Get-Date).AddDays(-15) ) -and (! $_.PSIsContainer)} | select name| Remove-Item -Verbose -Force -Recurse -ErrorAction SilentlyContinue
If you are having problems with the above examples on a Windows 10 box, try replacing .CreationTime with .LastwriteTime. This worked for me.
dir C:\locationOfFiles -ErrorAction SilentlyContinue | Where { ((Get-Date)-$_.LastWriteTime).days -gt 15 } | Remove-Item -Force
Another alternative (15. gets typed to [timespan] automatically):
ls -file | where { (get-date) - $_.creationtime -gt 15. } | Remove-Item -Verbose
#----- Define parameters -----#
#----- Get current date ----#
$Now = Get-Date
$Days = "15" #----- define amount of days ----#
$Targetfolder = "C:\Logs" #----- define folder where files are located ----#
$Extension = "*.log" #----- define extension ----#
$Lastwrite = $Now.AddDays(-$Days)
#----- Get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $Targetfolder -include $Extension -Recurse | where {$_.LastwriteTime -le "$Lastwrite"}
foreach ($File in $Files)
{
if ($File -ne $Null)
{
write-host "Deleting File $File" backgroundcolor "DarkRed"
Remove-item $File.Fullname | out-null
}
else {
write-host "No more files to delete" -forgroundcolor "Green"
}
}
$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"
# Delete files older than the $limit.
Get-ChildItem -Path $path -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force -Recurse
This will delete old folders and it content.
The following code will delete files older than 15 days in a folder.
$Path = 'C:\Temp'
$Daysback = "-15"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item