Powershell find users expiring in 7 days

Update: You can get the accounts if you pass a string value, passing an integer initializes the timespan to 7 ticks!

Search-ADAccount -AccountExpiring -TimeSpan "7"

other valid options:

Search-ADAccount -AccountExpiring -TimeSpan (New-TimeSpan -Days 7)
Search-ADAccount -AccountExpiring -TimeSpan ([TimeSpan]::FromDays(7))

Could be a bug, it doesn't work for me as well. Here's a workaround:

$NeverExpires = 9223372036854775807
$ExpringIn = (Get-Date).AddDays(7) 

Get-ADUser -Filter * -Properties accountExpires | 
Where-Object {$_.accountExpires -ne $NeverExpires  -and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -lt $ExpringIn }

The attribute in use is accountExpires and is express in pacquet of 100 nano second since 1600

PS C:\Windows\system32> Get-ADuser user1 -Properties accountExpires


accountExpires    : 129821976000000000
DistinguishedName : CN=user1 users,OU=OUTest,DC=dom,DC=fr
Enabled           : True
GivenName         : user1
Name              : user1 users
ObjectClass       : user
ObjectGUID        : b1bef798-8e36-45ff-ad11-e79f89769efc
SamAccountName    : user1
SID               : S-1-5-21-3115856885-816991240-3296679909-1146
Surname           : Users
UserPrincipalName : [email protected]

you can convert it to [dateTime] like this :

PS> [datetime](Get-ADuser user1 -Properties accountExpires).accountExpires

mardi 22 mai 0412 22:00:00

Although this is an old thread.. Let me add a quick note and word of caution..

Becareful asking for accounts that are 7 days old. 7 days and 2 hours won't be 7 days and therefore won't match the query (might be why your CSV is empty).

You will therefore always want to say account that are more then 7 days, and less then 8 (etc) to catch all that are within the 7th day. etc...

Additionally, the code above
[datetime](Get-ADuser user1 -Properties accountExpires).accountExpires
give me an error
Cannot convert value "9223372036854775807" to type "System.DateTime". Error: "Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks"

You might also review http://social.technet.microsoft.com/Forums/scriptcenter/en-US/b70113b1-a043-4543-afa0-dbba5757d035/powershell-windows-2008-getaduser-accountexpirationdate-returns-wrong-result?forum=ITCG