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…
Comments on this entry are closed.
I was unable to get this working until I added a WMI type cast.
$InstalledDate = ([WMI]”)$OS.ConvertToDateTime($OS.InstallDate)
$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?
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.
How can we get this to write to a csv file?
Get-content .\SomeMachines.txt | .\Get-InstallDate.ps1
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
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?
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:\>
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?
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”
@Chris, that should be the way. Glad to know that you achieved it.
Nice script, saved me hours of work!
Thanks for the comment.
I am trying to pull a report of workstations created in the last 90 days to a txt file or csv. Any suggestions?
You can try below code.
Get-ADComputer -Filter * -Properties whenCreated | ? { ((Get-Date) – $_.whenCreated).Days -lt 90} | Format-Table Name,WhenCreated,Name,DistinguishedName -Autosize -Wrap
Source: http://social.technet.microsoft.com/wiki/contents/articles/5819.ad-powershell-for-active-directory-administrators.aspx
When i run the below commmand there is noting in CSV file …any reason ?
Get-content .\SomeMachines.txt | .\Get-InstallDate.ps1 | export-csv c:\temp\details.csv -notypeinformation
Is there any error when you run this command alone? As long as you see output with below command, it should export when you pipe it to export-csv
Get-content .\SomeMachines.txt | .\Get-InstallDate.ps1