Dummy n°2 learning Powershell Please help

hello expert I’m learning powershell and I need to create a script that compare actual date and bootdate of my windows clients.

I write this script:


# Get Current Date
$Todaydate = Get-Date 

# Get Last boot date
$lastbootdate = systeminfo | find "Tempo di avvio sistema:" 

# Compare two dates and Make an action
if($todaydate -gt $lastbootdate) {
Write-Output "Run script.exe"

else {
Write-Output "EXIT"

and I get this error:

Cannot compare "05/04/2023 16:35:46" with "Tempo di avvio sistema: 03/29/2023, 08:45:19". Error: "Could not convert value 'Time of
System Alert: 03/29/2023, 08:45:19" in type 'System.DateTime'. Error: 'String not recognized as a valid DateTime value. Word detected
unknown starting at index 0.""
In C:\Users\ataglietti\Desktop\check last reboot.ps1:11 car:4
+ if($datetoday -gt $lastbootdate) {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo: InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : ComparisonFailure

I think I need to truncate the $lastbootdate output from:
PS C:\WINDOWS\system32> systeminfo | find “Tempo di avvio sistema:”
Tempo di avvio sistema: 29/03/2023, 08:45:19

29/03/2023, 08:45:19

How can I fix it?
Hope all is clear , excuse me for my basic english
Angelo (from Italy)

Welcome to the forum. :wave:t3:

Since you did not format your code properly as code your question is hard to read and it is hard to distinguish between code and prosa.

So before we proceed … please go back, edit your question once again and fix the formatting of your code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

May I ask how? Do you have a book? … a tutorial? … youtube videos? …

Whatfor? At the moment you check if the client you’re running this code on has been started before you run this code. This will ALWAYS be the case. :wink:

This does NOT just get the current date - it gets the current date AND TIME. :point_up_2:t3:

The commands you use are actually unsuitable for the purpose I think you’re trying to serve.

In PowerShell we’re working with objects and properties. That’s why it’s a bad idea to use external tools like find in PowerShell. They do not return objects and that’s why PowerShell fails to compare a string data to a [DateTime] object.

A better approach would be to use native PowerShell cmdlets.

$Now = Get-Date 
$LastBootupTime = Get-CimInstance -ClassName CIM_OperatingSystem

if ($Now -gt $LastBootupTime.LastBootUpTime) {
    "This system has been started earlier than $($Now)"

What is it actually what you’re trying to do? If you want to check if the system has been started before today you may use the following approach:

$Now = Get-Date 
$LastBootupTime = Get-CimInstance -ClassName CIM_OperatingSystem

if ($Now.Date -gt $LastBootupTime.LastBootUpTime.Date) {
    "This system has been started earlier than $($Now.Date)"
1 Like