I'm looking into building a script that will get me both a date and parent directory of files created during a certain period.
So far this is what I've come up with:
get-childitem –recurse | where-object {($_.lastwritetime -gt “7/1/2013”) -and ($_.lastwritetime -le “7/22/2013”) }
I'm a bit clueless as to how to separate "Directory" and "LastWriteTime" (minus the time) into variables.
Would appreciate the help.
Thanks!
I wouldn't put them into separate variables. I'd just select the 2 properties:
$files = Get-ChildItem –Recurse | ? {
-not $_.PSIsContainer -and
$_.LastWriteTime -gt "7/1/2013" -and
$_.LastWriteTime -le "7/22/2013"
} | select Directory, #{n='LastWriteDate';e={Get-Date -uformat "%m\/%d\/%Y"}}
Then you can access those values like this:
$files[0].Directory.FullName
$files[0].LastWriteDate
Related
Below command, delete files older than 30 days
Get-ChildItem –Path "C:\path\to\folder" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item
But how to add filters, that dont delete files if
date is 1st of each month or
date is 15th of each month or
date is 30
also ignore files with name '%weekly%'
Since you only want to remove files, you should use the -File switch on the Get-ChildItem.
In order not to keep calculating the reference date 30 days ago, I like to define that in a variable $refDate first.
Also, you should use the date, as of midnight by setting the time part to 00:00:00. That is where property Date comes in.
$refDate = (Get-Date).AddDays(-30).Date # set it to midnight
Get-ChildItem -Path "C:\path\to\folder" -File -Recurse |
Where-Object { ($_.LastWriteTime -lt $refDate) -and
($_.BaseName -notlike '*%weekly%*') -and
(1,15,30 -notcontains $_.LastWriteTime.Day)} |
Remove-Item -WhatIf
P.S. I have added the -WhatIf switch, so you can see what would happen first. If you are satisfied with the messages in the console, remove this switch to actually start deleting files
You can go for powershell script like below:
$FolderName = "c:\dev\test"
foreach($file in (GEt-childitem -Path $FolderName -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} ))
{
if (($file.lastwritetime.Date.Day -in 1,15,30 ) -or ($file -like '*weekly*'))
{
continue
}
Remove-Item -Path $file.FullName
}
I'm sure I've missed something obvious, but it's been a while since I have needed to use PowerShell (n.b. it is version 2).
I need a basic script that deletes files over a certain age (3 days). I have the following:
$logDirectory = "C:\logs\"
$days = (Get-Date).AddDays(-3)
# Delete files older than the $days
Get-ChildItem -Path $logDirectory -Recurse |
Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $days } |
%{Write-Host File Found: $_.fullname $_.LastWriteTime}
Get-ChildItem -Path $logDirectory -Recurse |
Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $days } |
Remove-Item -Force
This works, but If I combine the two it doesn't. And I'm sure there must be a neater way to do this where I can write out a list of files, and then delete them. Something like:
Get-ChildItem -Path $logDirectory -Recurse |
Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $days } |
%{Write-Host File Found: $_.fullname $_.LastWriteTime} |
Remove-Item -Force
But all this does is list the items, not delete them.
I am currently working on a script that will count all of the directories (within a specified path, \\servername\logs\failures) which have a naming convention of "P0*" or "MININT*" - but the folders named as "MININT*" also must have a subfolder that contains a specified log file. Also, it should only be counting folders created in the last 24 hours.
Here is my current code, it keeps returning a zero value and I am not sure why. I have been searching for hours and tried different methods using recurse, Test-Path, etc. to no avail.
$imagefailures = Get-ChildItem '\\servername\logs\failures' -Directory |
Where-Object {
($_.Name -like "P0*") -or
(($_.Name -like "MININT*") -and (Test-Path "\WinPE_TS_X_Win\SMSTSLog\Get-Name.log")) -and
($_.LastWriteTime -gt (Get-Date).AddHours(-24))
} | Measure-Object | select -ExpandProperty Count
Try this:
$imagefailures = Get-ChildItem '\\servername\logs\failures' -Directory |
Where-Object {
($_.Name -like "P0*") -or
(($_.Name -like "MININT*") -and (Test-Path "$($_.FullName)\WinPE_TS_X_Win\SMSTSLog\Get-Name.log")) -and
($_.LastWriteTime -gt (Get-Date).AddHours(-24))`
} | Measure-Object | select -ExpandProperty Count
The path you are testing will always just be "\WinPE_TS_X_Win\SMSTSLog\Get-Name.log" if you do not append it to the folder path you are iterating on.
I created a script that allows me to search for and ignore directories from a Remove-Item statement, and the script works, but not necessarily to the extent I need it to.
Get-ChildItem -Path $Path |
Where-Object {
($_.LastAccessTime -lt $Limit) -and
-not ($_.PSIsContainer -eq $True -and $_.Name -contains ("2013","2014","2015"))
} | Remove-Item -Force -Recurse -WhatIf
This script is currently finding and deleting all objects that
Have not been accessed in the given time period
But what I need this script to do is find and delete all objects that
Have not been accessed in the given time period AND
Exclude directories that contain the name of "2013", "2014", or "2015".
I'm not arguing that the script "isn't working properly", but the thesis of my question is this:
How do I program this script to look at the directory name first, and then the last access date? I don't know where and how to tell this script that the $_.Name needs to take precedence over the $_.LastAccessTime -lt $Limit.
Currently the logic of your condition is this:
Delete objects that were last accessed before $Limit and are not folders whose name contains the array ["2013","2014","2015"].
The second condition is never true, because a string can never contain an array of strings.
Also, the last modification time is stored in the LastWriteTime property.
What you actually want is something like this:
Where-Object {
$_.LastWriteTime -lt $Limit -and
-not ($_.PSIsContainer -and $_.Name -match '2013|2014|2015')
}
If the directory names consist only of the year and nothing else you could also use this:
Where-Object {
$_.LastWriteTime -lt $Limit -and
-not ($_.PSIsContainer -and '2013','2014','2015' -contains $_.Name)
}
Note the reversed order of the last clause (array -contains value).
I am writing a script that checks, recursively, all folders and file names in a directory, and then returns the names and last write times to a text file. I only want the names of files and folders that have been added within the past twenty four hours.
$date = Get-Date
Get-ChildItem 'R:\Destination\Path' -recurse |
Where-Object { $_.LastWriteTime -lt $date -gt $date.AddDays(-1) } |
Select LastWriteTime, Name > 'C:\Destination\Path\LastWriteTime.txt' |
Clear-Host
Invoke-Item 'C:\Destination\Path\LastWriteTime.txt'
The .txt file that is invoked is blank, which, based on the test conditions I have set up should not be the case. What am I doing wrong?
You are missing a logical and. Change:
Where-Object { $_.LastWriteTime -lt $date -gt $date.AddDays(-1) }
to
Where-Object { $_.LastWriteTime -lt $date -and $_.LastWriteTime -gt $date.AddDays(-1) }
Even better to use parenthesis, if you would have used them then the syntax would not have been parsed with the missing AND:
Where-Object { ($_.LastWriteTime -lt $date) -and ($_.LastWriteTime -gt $date.AddDays(-1)) }