How to get stale computers from Active Directory

Copy the text below:

*****
$DaysInactive = 90

$time = (Get-Date).Adddays(-($DaysInactive))

Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name,
OperatingSystem, SamAccountName, DistinguishedName | Export-CSV “C:\StaleComps.CSV” –NoTypeInformation
*****
Save as stale.ps1 then run from Powershell

 

If you receive the following error:

The term ‘Get-ADComputer’ is not recognized as the name of a cmdlet, function, script file, or operable program.
Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\super\desktop\Aged.ps1:5 char:15
+ Get-ADComputer <<<< -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties
Nam
e, OperatingSystem, SamAccountName, DistinguishedName | Export-CSV “C:\StaleComps.CSV” -NoTypeInformation
+ CategoryInfo : ObjectNotFound: (Get-ADComputer:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Run this command:

PS C:\Users\super\desktop> import-module activedirectory

If you receive a message concerning unsigned code, Set Execution Policy:
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned