none
Powershell script to get local user accounts and disable stale (over 90 day) RRS feed

  • Question

  • I have a script that will output the local users who have not logged in 90+ days. 

    Can I pipe the results to the Disable-LocalUser?

    Could I just add code to the script to take username results and run the Disable-LocalUser function?

    Here is my get info code:

    $Today = (Get-Date).Date $MaxDaysOld = 90 $Results = Get-LocalUser -Name * | Where-Object { $_.Enabled } | ForEach-Object { if ($_.LastLogon) { $LastLogon = $_.LastLogon $DaysOld = ($Today - $_.LastLogon).Days } else { $LastLogon = $DaysOld = '_Never_' } if ($DaysOld -eq '_Never_' -or $DaysOld -gt $MaxDaysOld) { [PSCustomObject]@{ Name = $_.Name LastLogon = $LastLogon DaysAgo = $DaysOld } } } $Results

    Thanks 

    Wednesday, August 14, 2019 5:01 PM

All replies