Hi, I’d like to get all users in AD that haven’t logged on in the last 90 days. I have a script but it always throws mr an error and can’t get the results. Could some help where my mistake is? Many thanks to the community
Function to convert the last logon timestamp to a readable format
function Convert-LastLogon
{
param (
[Parameter(Mandatory = $true)]
[int64]$lastLogonTimestamp
)
return [DateTime]::FromFileTime($lastLogonTimestamp)
}
Set the output file path
$outputFilePath = “C:\temp\lastlogon90days.csv”
Get all domain controllers in the current domain
try
{
$domainControllers = Get-ADDomainController -Filter *
}
catch
{
Write-Host “Error occurred while getting domain controllers: $_” -ForegroundColor Red
exit
}
Initialize an empty array to store the results
$results = @()
Iterate through each domain controller
foreach ($dc in $domainControllers)
{
# Query for enabled users that have not logged in for 90 days on the current domain controller
try
{
$users = Get-ADUser -Filter { Enabled -eq $true -and LastLogonTimeStamp -lt $((Get-Date).AddDays(-90).ToFileTime()) } -Server VM-PRD-DC-W04
}
catch
{
Write-Host “Error occurred while querying users on $($dc): $_” -ForegroundColor Yellow
continue
}
# Iterate through each user
foreach ($user in $users)
{
# Get the last logon timestamp of the user
$lastLogon = Convert-LastLogon -lastLogonTimestamp $user.LastLogonTimestamp
# Add the user details to the results array
$results += [PSCustomObject]@{
"Username" = $user.SamAccountName
"LastLogonDate" = $lastLogon
"DomainController" = $dc.DNSHostName
}
}
}
Export the results to a CSV file
$results | Export-Csv -Path $outputFilePath -NoTypeInformation
The error I get:
Error occurred while querying users on MYDCNAME-W04: Cannot process argument because the value of argument “path” is not valid. Change the value of the “path” argument and run the operation again.