Techibee.com

PowerShell: How to get OS installed date of any windows computer

Sometimes we want to know when a computer is built. There are different places where we can check to find out the proximate information. Examples include, computer object creation datetime in active directory, build logs if you use any OS deployment softwares, creation date of c:\windows folder, and a WMI query if you are scripting geek etc. Since WMI provides most accurate information about when a Operating System is installed, I wrote a quick script to solve one of my requirement where I want to get installed date of bunch of computers.

Here is the script.

[cmdletbinding()]
param (
 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
 [string[]]$ComputerName = $env:computername
)            

begin {}
process {
 foreach ($Computer in $ComputerName) {
  if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
   Write-Verbose "$Computer is online"
   $OS    = Get-WmiObject -Class Win32_OperatingSystem -Computer $Computer
   $InstalledDate = $OS.ConvertToDateTime($OS.Installdate)
   $OutputObj  = New-Object -Type PSObject
   $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer
   $OutputObj | Add-Member -MemberType NoteProperty -Name InstalledDate -Value $InstalledDate
   $OutputObj
  } else {
   Write-Verbose "$Computer is offline"
  }            

 }
}            

end {}

You can use this script in variety of ways. Below are the some of the usage examples. Save the above code into Get-InstallDate.ps1 file before you try them.

[PS] C:\>.\Get-InstalledDate.ps1 — get installed date of local computer

[PS] C:\>.\Get-InstalledDate.ps1 -ComputerName “MYtestpc1” — get installed date of local computer

[PS] C:\>”mytestpc1″, “mytestpc2” | .\Get-InstalledDate.ps1 — if you want to do it for multiple computers

[PS] C:\>Get-content .\comps.txt | .\Get-InstalledDate.ps1 — if you want to read the computers list from a text file and find out the information.

Hope this helps…

 

 

Exit mobile version