ADScripts/Disable-OldPC.ps1

31 lines
1.3 KiB
PowerShell
Raw Normal View History

2023-09-14 11:13:58 -06:00
import-module ActiveDirectory
if (!($cred)) {
$cred = Get-Credential -Message "Enter your domain admin login:"
}
2023-09-29 10:49:37 -06:00
$AgedOutDays = -270 #store this as a negative number. How many days back should the PC have changed its password
2023-10-02 15:40:36 -06:00
$CSVPATH = "C:\Users\eeckert\OneDrive - MPE Engineering Ltd\Documents\Downloads\Blah.csv"
2023-09-29 10:49:37 -06:00
$computers = get-adcomputer -filter { (Enabled -eq $True) } -properties passwordlastset | where passwordlastset -le (get-date).adddays($AgedOutDays)
2023-09-14 11:13:58 -06:00
foreach ($computer in $computers) {
$Output = 'Moving ' + $computer.name + ' to disabled OU.'
Write-Output $Output
$DescText = 'Disabled: ' + (get-date)
$computer | set-adcomputer -Enabled $false -Credential $cred -Description $DescText
$computer | move-adobject -TargetPath "OU=Computers,OU=Disabled Objects,DC=mpe,DC=ca" -Credential $cred
}
2023-10-02 15:40:36 -06:00
$CSVList = import-csv $CSVPATH -ErrorAction SilentlyContinue #get list of hostnames
2023-09-14 11:13:58 -06:00
2023-10-02 15:40:36 -06:00
foreach ($computer in $csvlist) {
$Output = 'Moving ' + $computer.hostname + ' to disabled OU.'
Write-Output $Output
$computer = Get-ADComputer $computer.hostname
$DescText = 'Disabled: ' + (get-date)
$computer | set-adcomputer -Enabled $false -Credential $cred -Description $DescText
$computer | move-adobject -TargetPath "OU=Computers,OU=Disabled Objects,DC=mpe,DC=ca" -Credential $cred
}
2023-09-14 11:13:58 -06:00