# Get Files modified after a certain date

**URL:** <https://forums.powershell.org/t/get-files-modified-after-a-certain-date/25100>\
**Category:** PowerShell Help\
**Created:** [December 2, 2024, 4:51pm UTC](https://forums.powershell.org/t/get-files-modified-after-a-certain-date/25100 "2024-12-02T16:51:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xwr](https://avatars.discourse-cdn.com/v4/letter/x/8491ac/32.png) [@xwr](https://forums.powershell.org/u/xwr)\
**Post date:** [December 2, 2024, 4:52pm UTC](https://forums.powershell.org/t/get-files-modified-after-a-certain-date/25100/1 "2024-12-02T16:52:00Z")

</div>

Hi,  
I try to get all files of a folder which have been modified after a certain date. I know this question has already been asked before, and I found several solutions / proposals, but none worked. And concerning the particular behaviour I encountered, I found nothing, maybe someone can help?

To test, I have a folder with three files, two with lmd in 07/24, one with 09/24. I run the same program, once to find files modified after 01-07-24 (locale is Germany, so dd-mm-yy) and it works, as expected the three files are found. I run it again with the date of 01-08-24 and the output becomes completely weird.

Here the program:

```auto
$sourceFolder = "d:\test"
#sourceFolder contains 3 files, 2 with date 07/24, one with date 09/24

Function List-Files{
	
	$fn = Get-ChildItem $sourceFolder -File | ? LastWriteTime -gt $ld 
	$sourceFiles=$fn.FullName
	Write-Host $sourceFiles.Length
	Write-Host $fn.Length

	if($sourceFiles -eq $null){
		Write-Host "null"
		exit
	}

	for($k=0; $k -lt $sourceFiles.Length; $k++){
		Write-Host $sourceFiles[$k]
	}
	Write-Host " ************************"
}

$ld = Get-Date("01-07-24")
List-Files
$ld = Get-Date("01-08-24")
List-Files

```

and here the output

```auto
3
3
D:\test\file_0107.txt
D:\test\file_1307.txt
D:\test\file_1509.txt
************************
21
657
D
:
\
t
e
s
t
\
f
i
l
e
_
1
5
0
9
.
t
x
t
************************

```

Any idea?  
Thanks!

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 2, 2024, 6:08pm UTC](https://forums.powershell.org/t/get-files-modified-after-a-certain-date/25100/2 "2024-12-02T18:08:38Z")

</div>

When you search for files with the date 01-08-24 you’re getting only one result. So the `Length` property is the number of characters in the string, rather than the number of elements in an array. So your `for` loop is printing each character in the string, one-by-one.

If you want to keep your code as it is, declare `$sourceFiles` to be of type `array`.

```PowerShell
[array]$sourceFiles = $fn.FullName

```

You don’t need to loop over the files to print them to the console though. You can just return them with:

```PowerShell
$sourceFiles

```

A couple of other pointers:

Avoid using aliases such as `?` in scripts and code that you’re sharing with other people.

It’s considered a PowerShell best practice to place `$null` on the left as placing it on the right doesn’t always yield the expected result:

> **[Everything you wanted to know about $null - PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-null?view=powershell-7.4)**
>
> The PowerShell $null often appears to be simple but it has a lot of nuances. Let's take a close look at $null so you know what happens when you unexpectedly run into a null value.

---

<div class="post-metadata">

**Author:** ![xwr](https://avatars.discourse-cdn.com/v4/letter/x/8491ac/32.png) [@xwr](https://forums.powershell.org/u/xwr)\
**Post date:** [December 2, 2024, 6:41pm UTC](https://forums.powershell.org/t/get-files-modified-after-a-certain-date/25100/3 "2024-12-02T18:41:27Z")

</div>

@matt-bloomfield : Thank for the reply, explanations and hints! Works.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [January 1, 2025, 6:41pm UTC](https://forums.powershell.org/t/get-files-modified-after-a-certain-date/25100/4 "2025-01-01T18:41:37Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
