26 October 2012

Find Old Computers in AD

For Windows 2003/2008
(Must be 2003 Native domain or newer)
In a Command Prompt window type:
dsquery computer -inactive <num>
Where <num> is the minimum number of weeks the device has been inactive for. Advice seems to be to use at least 2, as anything less than that is not fully reliable.

For Windows 2008 R2+
In a PowerShell  window type:
$time=Read-host "Enter a date in format mm/dd/yyyy"
then
Get-ADComputer -Filter * | Get-ADObject -Properties lastlogontimestamp | where {(([DateTime]::FromFileTime($_.lastlogontimestamp) - ([system.datetime]$time)).totaldays) -lt 0 } | select name

You can change what you include in the select command at the end to get more or different info.

If you want to then remove all resulting computer accounts use the following command:
Remove-ADObject -recursive
in place of the select name

That is, to remove all computers older than $time use the following command:
Get-ADComputer -Filter * | Get-ADObject -Properties lastlogontimestamp | where {(([DateTime]::FromFileTime($_.lastlogontimestamp) - ([system.datetime]$time)).totaldays) -lt 0 } | Remove-ADObject -recursive
For more detail or a better example (of which this is basically a cut and paste at the moment), see reference site below.

References:
http://blog.mattvogt.net/powershell-last-logon-timestamp-for-single-ho 

10 October 2012

Reset Offline Files Cache in Windows 7

To reset the offline files cache in Windows 7

  1. Create a registry file called resetcache.reg file with the following contents:

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\CSC\Parameters] "FormatDatabase"=dword:00000001
  2. Double click the file and it should merge with registry. Alternatively, use command line:
    E.g. regedit.exe /s resetcache.reg
  3. Reboot the PC

Reference:
http://www.networknet.nl/apps/wp/archives/1093

Find the Desktop

It can be REALLY hard to access the desktop using scripts, especially batch scripts, if it has been moved from the default location of C:\Users\%username%, or even worse if it has been moved out of the %userprofile% location by the way of Folder Redirection.

As batch files cannot find this location, and I do not want to start learning VB script, I want a Powershell method I can use to find it.

This is the method I found:

$desktop = [Environment]::GetFolderPath("Desktop")

The $desktop variable now contains the location to the currently logged-in user's desktop. To view this path simply type

write-host $desktop

Happy day!

Reference:
The comment on the post at http://stackoverflow.com/questions/11349885/get-currently-logged-in-users-with-powershell-to-add-shortcut-to-desktop

:-)