≡ Menu

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…

 

 

{ 14 comments… add one }
  • Colin September 9, 2012, 11:47 am

    I was unable to get this working until I added a WMI type cast.

    $InstalledDate = ([WMI]”)$OS.ConvertToDateTime($OS.InstallDate)

  • Sitaram Pamarthi September 9, 2012, 1:51 pm

    $OS is already of WMI Management Object type. No need to create explicit instance of another WMI instance. What was the error you were getting with the above code?

  • Brendan February 19, 2013, 8:44 pm

    When running a list of servers through your script some error-ed out with “Get-WmiObject : Invalid namespace.” I’m guessing that a system in the list was unreachable but after looking through the list I’m not sure when servers spit out the error.

  • AnonyMouse September 17, 2014, 9:30 pm

    How can we get this to write to a csv file?

    Get-content .\SomeMachines.txt | .\Get-InstallDate.ps1

    • Sitaram Pamarthi October 25, 2014, 9:48 am

      Sorry for replying late. You can use export-csv cmdlet to do that.

      Get-content .\SomeMachines.txt | .\Get-InstallDate.ps1 | export-csv c:\temp\details.csv -notypeinformation

  • Chris November 17, 2014, 8:45 am

    Great script, thank you! Since I’m very new to PS, how could this be tweeked to check if install date is AFTER a certain date or number of days?

    • Sitaram Pamarthi November 17, 2014, 6:34 pm

      Here is the simple code to find out how many days back OS is installed.

      PS C:\> $OS = Get-WmiObject -Class Win32_OperatingSystem
      PS C:\> $OSdate = $OS.ConvertToDateTime($OS.InstallDate)
      PS C:\> ((Get-Date) – $OSdate).Days
      151
      PS C:\>

  • Chris November 18, 2014, 5:26 am

    Thank you Sitaram, so could all results be sent to a txt or csv ? All PC’s in AD that have install of windows in the last 260 days?

  • Chris November 18, 2014, 12:24 pm

    Guys, this is what I’ve managed so far.

    I am doing the following… Generating a list of all computers in a particular OU

    cycling through them, getting the install date, and I then want to store and output this information. This is the code, but it seems it’s failing somewhere, my csv only has one record.

    $Computers=get-QADComputer -SearchRoot “OU=Desktops,OU=Computers,OU=blah,DC=blah”

    foreach ($Computer in $Computers) {
    if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
    Write-Verbose “$Computer is online”
    $comp = $Computer.name
    $OS = Get-WmiObject -Class Win32_OperatingSystem -Computer $comp
    $InstalledDate = $OS.ConvertToDateTime($OS.Installdate)
    $days = ((get-date) – $InstalledDate).days
    $OutputObj = New-Object PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name “ComputerName” -Value “$comp”
    $OutputObj | Add-Member -MemberType NoteProperty -Name “InstalledDate” -Value “$InstalledDate”
    $OutputObj | Add-Member -MemberType NoteProperty -Name “Num of Days” -Value “$days”
    $OutputObjs += $OutputObj
    } else {
    Write-Verbose “$Computer is offline”}

    }
    $OutputObjs | Export-Csv -NoTypeInformation -Path “c:\OutputObjs.csv”

  • Hendrik October 24, 2015, 2:15 pm

    Nice script, saved me hours of work!

  • Michael R Krumwiede August 19, 2016, 12:12 am

    I am trying to pull a report of workstations created in the last 90 days to a txt file or csv. Any suggestions?

Leave a Comment