Powershell get-childitem excludes a range of dates

powershell get-childitem files only
powershell get-childitem exclude folders
powershell get-childitem directory
powershell list all files in directory and subdirectories
powershell get-childitem filter multiple
powershell get-childitem foreach
powershell get-childitem filename
powershell get-item

I'm using Get-ChildItem to get all of the tomcat log files where the date is not equal to the current date/today. I would like to get the tomcat log files where the date is not in a range of dates. For example the filenames of the last 7 days shall not be listed.

Sample of tomcat logs filename: catalina.2018-12-21.log host-manager.2018-12-21.log

$date=Get-Date (get-date).addDays(-0) -UFormat "%Y-%m-%d"
$file=Get-ChildItem "C:\tomcat\logs" -exclude "*$date*"

foreach($files in $file)
    Move-Item -Path $files -Destination "C:\expiredlogs"

[.....]Get all of the logs filename where date is not in the last 7 days range from "C:\expiredlogs"

Is there any good, efficient way to retrieve all the filenames not in the range 7 days ago till now?

I'm assuming that you just want to get all files, regardless of their name. Until now you based that on the file name itself, but you could base the search on the attributes of the files. In this example I'm getting all files that is 7 days old or newer.

$files=Get-ChildItem "C:\tomcat\logs" | Where-Object LastWriteTime -gt (Get-Date).AddDays(-7).Date

foreach($file in $files)
    Move-Item -Path $file -Destination "C:\expiredlogs"

The above code will only look at the write time for the file, regardless of the file name. You would limit that further if needed, by applying other filters.

Updated based on the recommendations from @LotPings

Get-ChildItem, Locations are exposed to Get-ChildItem by PowerShell providers. A location can be a file Example 5: Get child items using the Exclude parameter PS> Get-​ChildItem /etc/r* Directory: /etc UnixMode User Group LastWriteTime Size Name​  i want to create a .csv list of some folders in a giant directory (1400+ folders and subfolders, more than 6TB). I need to list the Fullname, creation date, last access time and last write time.

If you insist on using the file names you need to parse the names to dates, since Get-ChildItem doesn't know about dates. Something like this should do the trick:

Get-ChildItem "c:\programdata\dgs\cathi\log" | `
where { ([DateTime]::ParseExact($_.Name.Substring($_.Name.Length-14,10),'yyyy-MM-dd', $null) -lt (Get-Date).addDays(-7))} 

The number 14 is not a magical number, it's the length of the date + '.log'.

PowerShell Core for Linux Administrators Cookbook: Use PowerShell , Use PowerShell Core 6.x on Linux to automate complex, repetitive, and time-​consuming tasks Prashanth The automatic variable, $_, contains the object returned by Get-ChildItem, so that Where-Object can process it. DateTime or DateTime. There are situations where we could group the objects that were given as  Get-ChildItem 'C:\path\to\files\raw\*.xml' -Recurse | Second, convert your comparison of dates to datetime objects. Otherwise you are comparing DateTime objects from LastWriteTime to System.String. Powershell will attempt to convert but gets it wrong sometimes due to locale formatting differences.

Above mentioned method to use LastWriteTime is correct way to do this. But if there's time stamps in the filenames as you have, filtering could be more efficient than Where-Object and you can give it arrays.

First create an array of dates which should be excluded:

$range =  -3 .. -5 | ForEach-Object { "*$(Get-Date (Get-Date).addDays($_) -UFormat '%Y-%m-%d')*" }

Which today returns:


And pass that to the Get-ChildItem:

Get-ChildItem "C:\tomcat\logs" -Exclude $range

Windows PowerShell Cookbook: The Complete Guide to Scripting , Instead, the correct command is: Get-ChildItem $env:WINDIR\* -Include *.log Or to exclude certain directories from a search, use the following: Get-ChildItem  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more get-childitem sort-object : how to sort by CreationDate

Mastering System Center 2012 Operations Manager, Get-SCXSSHCredential cmdlet, 593 Gibbs, Shawn, 398 global agent settings, 114 See also management group settings heartbeat, 112–113 security, manually A List, 167, 167 Create A Membership Formula page, 168–169, 169 Excluded See also APM Get-ChildItem cmdlet, 622 Get-Date cmdlet, 543–546, 583, 584  Get-ChildItem lets us list the items of one or more locations. This functionality is very similar to “dir” on Windows and “ls” on Unix-like systems. In fact, “dir” and “ls” are aliases that you can use indirectly in PowerShell in place of Get-ChildItem. Get-ChildItem has many parameters to help us find our desired results.

Professional Windows® PowerShell, Windows PowerShell typically supports aliases for the get—childitem cmdlet. However, since the get—childitem cmdlet can be used with a range of —path parameter. exclude — Filters out items among those specified by the value of the​  In this article, I want to show you a few tricks for working with dates in PowerShell. Retrieving the date. It is simple to make PowerShell display the current date. To do so, just enter the Get-Date cmdlet. If you need to display the date in a certain way, then PowerShell offers a number of different options for doing so.

Powershell script to remove files older than a certain date and log , $limit = (Get-Date).AddDays(-7),. $path = "C:\Example\Path". ) # Delete files older than the $limit. $FilesToDelete = (Get-ChildItem -Path $path -Exclude Logs  PowerShell GCI -Exclude Parameter. If you want to refine the output of Get-ChildItem (GCI), then consider the -Exclude parameter. This technique seems more reliable than using PowerShell’s -Include parameter. Topics for Get-ChildItem -Exclude. Example 1: To Exclude Particular Items; Example 2: To Exclude More Than One Item

  • IMO it makes more sense to use the plural $files for a set of files and the singular $file for the currently iterated one. Your get-date will include the current time, append a .Date or use Where-Object LastWriteTime -lt [datetime]::Today
  • Cheers @LotPings! Didn't read his code, I just adjusted it with my filter logic. Thx for pointing that out. Will try and up my game from now :)