V
V
Vertizo2020-03-31 14:39:25
PowerShell
Vertizo, 2020-03-31 14:39:25

Export list of users with expired password from AD?

Good afternoon!

I just can’t tame the script with exporting the list to a file of users with an expired password, everything is displayed correctly in the console, it’s impossible to export to a file, below is what is available:

$filename = Get-Date -Format yyyy.MM.dd
$exportcsv=”c :\ps\expired_password_” + $filename + “.csv”
$Users = Get-ADUser -SearchBase 'OU=xxxt,DC=xxx,DC=local' -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False } -Properties msDS-UserPasswordExpiryTimeComputed, PasswordLastSet, CannotChangePassword
foreach($user in $Users){
if( [datetime]::FromFileTime($user."msDS-UserPasswordExpiryTimeComputed") -lt (Get-Date)) {
$user.Name
}
}
Export-csv -InputObject $user -Path $exportcsv -Delimiter ";" -Encoding Unicode -NoTypeInformation

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
azarij, 2020-03-31
@Vertizo

how exactly does it not work? error is thrown? does not that infa go to the file? empty file? only one first user gets into the file?

S
strangerror, 2020-04-09
@strangerror

Solved a similar problem like this:

#Контейнер, в котором проверяем пользователей - значение distinguishedName
$UsersBase = 'OU=ХХХ,DC=ХХХ,DC=ХХХ,DC=ХХХ'

#Период неактивности пользователя в домене
$TimeSpan = 90
$TimeStamp = (Get-Date).AddDays(-($TimeSpan+14))

$logFile = "inactiveUsers_$(Get-Date -Format "dd.MM.yyyy").csv"

#Учетная запись актавна и LogonTimestamp наступило раньше, чем $TimeSpan; или lastLogonTimestamp пустое, но при этом запись создана более $TimeSpan дней назад
Get-ADUser -Filter {(Enabled -eq $true) -and ((lastLogonTimestamp -le $TimeStamp) -or ((lastLogonTimestamp -notlike '*') -and (whenCreated -le $TimeStamp)))} -SearchBase $UsersBase -Properties lastLogonTimestamp,whenCreated | select Name,SamAccountName,@{Name="lastLogonTimestamp";Expression={if ($_.lastLogonTimestamp -eq $null) {''} else {[datetime]::FromFileTime($_.lastLogonTimestamp)}}},whenCreated | sort -Property Name | Export-Csv $logFile -Encoding utf8 -NoTypeInformation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question