Streamlining IT Operations: PowerShell Script for Checking Windows OS Install Date

Key takeaways

  • Automates install date retrieval: The script efficiently fetches the Windows OS installation date, automating what would otherwise be a manual and time-consuming task.
  • Utilizes multiple data sources: It retrieves installation dates from the registry, system info, WMI, and PowerShell’s Get-ComputerInfo, ensuring comprehensive data collection.
  • Offers custom field output: The optional CustomField parameter allows for integration with existing data systems by saving the install date in a specified field.
  • Supports major windows versions: Designed for Windows 10 and Windows Server 2016, making it relevant for most modern Windows environments.
  • Error handling mechanisms: Includes try-catch blocks for each method of data retrieval, ensuring the script runs smoothly without interruptions.
  • Facilitates IT management and security: Helps IT professionals identify outdated systems, which is crucial for maintenance and security compliance.
  • Integration with management platforms: The script’s functionality highlights the benefits of integrating such tools into platforms like NinjaOne for comprehensive system management.

In the dynamic field of Information Technology, efficient management of system information is crucial. PowerShell scripts have become instrumental in simplifying complex tasks, with their ability to automate and accurately retrieve data. A notable example is a script designed to fetch the install date of a Windows operating system. This capability is not just a matter of record-keeping but an essential aspect of system management and security.

Background

The script in question serves an important function for IT professionals and Managed Service Providers (MSPs). It’s designed to retrieve the installation date of the Windows operating system on a machine. This information is vital for various reasons, including compliance, maintenance scheduling, and security auditing. Given that IT environments are increasingly complex, having a script to automate this task is immensely valuable.

The script:

#Requires -Version 5.1

<#
.SYNOPSIS
    Fetches the install date. Outputs to the activity feed and can store it in a custom field.
.DESCRIPTION
    Fetches the install date. Outputs to the activity feed and can store it in a custom field.

.EXAMPLE
    (No Parameters)
    ## EXAMPLE OUTPUT WITHOUT PARAMS ##
    Install Date: 08/18/2021 13:50:15

PARAMETER: -CustomField "InstallDate"
    A custom field to save the install date to.
.EXAMPLE
    -CustomField "InstallDate"
    ## EXAMPLE OUTPUT WITH CustomField ##
    Install Date: 08/18/2021 13:50:15
.OUTPUTS
    None
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes: Renamed script and added Script Variable support
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>

[CmdletBinding()]
param (
    [string]$CustomField
)

begin {
    $Epoch = [DateTime]'1/1/1970'
    if ($env:customFieldName -and $env:customFieldName -notlike "null") { $CustomField = $env:customFieldName }
    Write-Host ""
}
process {
    $InstallDate = $(
        try {
            # Get Install Date from registry
            Get-ChildItem -Path "HKLM:\System\Setup\Source*" -ErrorAction SilentlyContinue | ForEach-Object {
                $InstallDate = Get-ItemPropertyValue -Path Registry::$_ -Name "InstallDate" -ErrorAction SilentlyContinue
                [System.TimeZone]::CurrentTimeZone.ToLocalTime(($Epoch).AddSeconds($InstallDate))
            }
            $InstallDateCu = Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name "InstallDate" -ErrorAction SilentlyContinue
            [System.TimeZone]::CurrentTimeZone.ToLocalTime(($Epoch).AddSeconds($InstallDateCu))
        }
        catch {
            # Skip if errors
        }

        try {
            # Get Install date from system info
            $SystemInfo = systeminfo.exe
            # --- Output of system info ---
            # Original Install Date:     9/3/2020, 8:54:48 AM
            $($SystemInfo | Select-String "install date") -split 'Date:\s+' | Select-Object -Last 1 | Get-Date
        }
        catch {
            # Skip if errors
        }

        try {
            # Get Install date from WMI
            $(Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue).InstallDate
        }
        catch {
            # Skip if errors
        }

        try {
            if ($PSVersionTable.PSVersion.Major -ge 5 -and $PSVersionTable.PSVersion.Minor -ge 1) {
                $ComputerInfo = Get-ComputerInfo -Property WindowsInstallDateFromRegistry, OsInstallDate -ErrorAction SilentlyContinue
                $ComputerInfo.WindowsInstallDateFromRegistry
                $ComputerInfo.OsInstallDate
            }
        }
        catch {
            # Skip if errors
        }
    ) | Sort-Object | Select-Object -First 1

    if ($InstallDate) {
        if ($CustomField) {
            Ninja-Property-Set -Name $CustomField -Value $InstallDate
        }
        Write-Host "Install Date: $InstallDate"
    }
    else {
        if ($CustomField) {
            Ninja-Property-Set -Name $CustomField -Value "Unknown"
        }
        Write-Host "Install Date: Unknown"
    }
}
end {
    
    
    
}

 

Access over 300+ scripts in the NinjaOne Dojo

Get Access

Detailed breakdown

The script operates in several stages:

  • Parameter definition: It starts by defining an optional parameter CustomField. This allows the user to specify a custom field where the install date will be saved.
  • Initialization: It initializes a variable representing the Unix epoch (January 1, 1970) as a baseline for date calculations.
  • Data retrieval process: The script then employs multiple methods to fetch the install date:
    • Registry Entries: Checks multiple registry paths for the install date, converting the Unix timestamp to a local time format.
    • System Info Utility: Extracts the install date from the output of the systeminfo.exe command.
    • Windows Management Instrumentation (WMI): Retrieves the install date from the Win32_OperatingSystem class.
    • Computer Info (for PowerShell 5.1 and above): Utilizes the Get-ComputerInfo cmdlet for fetching installation dates.
  • Data selection and output: From the collected dates, it picks the earliest one as the most likely original installation date and either outputs it or sets it in a specified custom field.

Potential use cases

Imagine an IT administrator managing a fleet of computers in a large organization. By running this script, they can quickly ascertain which machines are running on older installations, potentially flagging them for updates or deeper security audits.

Comparisons

Traditional methods like manually checking system properties or sifting through system logs are time-consuming and prone to human error. This script automates and centralizes the process, making it both faster and more reliable.

FAQs

  • How is the script executed?
    Run it in a PowerShell environment, optionally specifying a CustomField for output.
  • Can it work on all Windows versions?
    It’s designed for Windows 10 and Windows Server 2016 and above.
  • Is it error-proof?
    The script contains multiple try-catch blocks to gracefully handle any errors during execution.

Implications

Accurate knowledge of OS install dates is critical for security compliance and system maintenance. Outdated systems can be security risks, and this script assists in identifying such systems for necessary updates.

Recommendations

  • Always test scripts in a controlled environment before deploying them in a production setting.
  • Customize the CustomField parameter according to your organization’s data structure for seamless integration.

Final thoughts

This script exemplifies how tools like NinjaOne can enhance IT operations. NinjaOne’s platform could integrate such scripts for broader system management, offering a comprehensive solution that streamlines IT operations, ensuring systems are up-to-date and secure. Leveraging PowerShell scripts in such platforms brings efficiency and accuracy to the forefront of IT management.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service deliver tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, check out a live tour, or start your free trial of the NinjaOne platform.

Categories:

You might also like

Watch Demo×
×

See NinjaOne in action!

By submitting this form, I accept NinjaOne's privacy policy.

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).