Build/enrollment date script requirement for Intune - Part of string to Date

Hi,

I’m trying to create a boolean test for intune that compares a machine’s build date to a specific date. If the build date is after the specific date return true and intune will then install an app.

The problem I have is systeminfo|find /i “install date” outputs to a sentance:

“Original Install Date: 18/06/2024, 17:21:19”

So far I have:

$buildDate = (systeminfo|find /i “install date”).ToString(‘dd/MM/yyyy’)
$compareDate = (date 01/12/25).ToString(‘dd/MM/yyyy’)

if($compareDate -ge $builddate) {
exit $True
}

else {
exit $False
}

Some possible solutions:

$buildDate = (systeminfo|find /i “install date”).Split(":",2)[1].Trim()
$buildDate = (systeminfo|find /i “install date”).SubString(22).Trim()

(systeminfo|find /i “install date”) -Match '^Original Install Date:\s*(.*)$'
$buildDate = $matches[1]

Give this a try:

$buildDate = (systeminfo|find /i "install date")
$bd = $buildDate -replace 'Original Install Date:\s+(\d?\d/\d?\d/\d\d\d\d),','$1'
$bd = Get-Date $bd
$compareDate = Get-Date "2025/01/25"
if ($compareDate -ge $bd{
    exit 1
}
else{
    exit 0
}

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

Rather than parse the output from SystemInfo, you can get the install date from CIM_OperatingSystem:

Get-CimInstance CIM_OperatingSystem | Select-Object InstallDate

You need to be careful when comparing dates as strings as you might not get the result you expect:

$buildDate = '18/06/2024'
$compareDate = '01/12/2025'
$compareDate -ge $buildDate
False

Instead, compare DateTime objects:

$buildDate = Get-Date (Get-CimInstance CIM_OperatingSystem | 
    Select-Object InstallDate).InstallDate
$compareDate = Get-Date '01/12/2025'
$compareDate -ge $buildDate
True
2 Likes

Thank you Matt, That is really helpful.

I will make sure to </> from now on!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.