Get-Date - showing the same date where its used in script?

So either I’m loosing my marbles (entirely likely) or Get-Date is working differently to how I understood it to work. I’ve been using it for years, and in the same/similar way - usually when creating log files, I’ll define a date/time stamp using get-date, and reference it on each log entry.

With my latest script, its a really simple one, and its behaving unexpectedly. For simplicity’s sake, I have replaced all script functions with a simple start-sleep timer to test this issue.

$date = get-date -format "[dd/MM/yy : HH:mm]"

Write-Host "Started at $($date)"

Start-Sleep 10

Write-Host "Ended at $($date)"

I was expecting the date to change when it calls the variable at the end of the script, however, it shows the same date and time as it does at the beginning. It seems instead of calling the variable when referenced, it runs it once at the start of the script and never again. I swear this wasnt how it used to work?

I have a massive server build/deployment script that uses Get-Date in exactly this way (although it adds-content to a log file instead) and it works as expected - i.e. it stamps the date/time when that particular command was run rather than at the start of the script.

Very confused.

Why should a variable change its value just because it’s outputted? Of course the value stays the same when you don’t update your variable. It is been set the moment you use the assignment operator = and the cmdlet Get-Date and it keeps this value until you change it. :wink:

You’re not referencing the command with the variable - you’re referencing the output the command provided at the moment of assignment of the variable.

Because I have apparently misunderstood something for many years about how variables are stored and called.

I thought that if a variable was referenced in a command, that it would run the command when referenced later in the script, rather than just store it once as a static value - in the case of Get-Date, seems a bit pointless, although the explanation does make sense.

For some reason it’s not working like this when I use this method in conjunction with “Add-Content” - that seems to update the date when called, even though I’ve defined the date variable in the same manner.

Any recommendations on the best/simplest way to achieve what I want here?

I doubt that it works this way - if I got you right. You may share your code to prove it!? :wink:

If you want to have new time stamp simply run the Get-Date cmdlet again. If you want to have the output in a certain format or to have some decorations around your date you could either use a function or a scriptblock. That’ll get executed everytime you call it.

For example like this:

function Out-TimeStamp {
    "`a"
    Get-Date -Format '[dd/MM/yy : HH:mm:ss]'
}
Out-TimeStamp
Start-Sleep -Seconds 95
Out-TimeStamp
1 Like

Literally just writing something like that, so thanks very much for the input :slight_smile:

Looking back - I’ve been off my rocker for the past 48 months apparently and my log files are all busted hah. Dont I feel special.

Well … I’m sorry for you. Sometimes learning is a painful process. But I’m sure you won’t make that mistake again. :wink:

And BTW: What I just realized now - your time stamp only has minutes in it. So you pretty likely even would not see a difference of some seconds this way!!! :wink:

HH:mm:ss is what I see, unless “ss” doesnt show seconds??

And yes, every day is a school day in the world of IT!

Yes. But in your initial question you only had this

$date = get-date -format "[dd/MM/yy : HH:mm]"

Oops, that would be a copy/pasta error lol.

Thanks again for the help mate.