Get Children From Different Time Zone

I have a remote machine running on UTC time. I need to use Get-Children to retrieve file properties. My script runs fine, but the LastWritetime is returned with the time being +6 hours (UTC time) - not the actual LastWritetime which is real file property. For example: File property 9/20/2004 9:06 PM PS returns 9/21/2004 3:06 AM — thus the +6 hours

Any help will be much appreciated, TIA Joe

The script is:

Invoke-Command -ComputerName Corvette -scriptblock{Get-ChildItem ‘\CORVETTE\CProject\MyProjects\Access’ -Recurse -Force |
select FullName , LastWriteTime, Mode | export-csv \corvette\c\fsync\LeftTemp.txt}

There are various ways to convert the time. This should work for you though. It’s using a calculated property so we can make the conversion at the same time we select the object:

Invoke-Command -ComputerName Corvette -scriptblock{
    Get-ChildItem ‘\CORVETTE\CProject\MyProjects\Access’ -Recurse -Force |
    select FullName,
           Mode,
           @{
                name='LastWriteTime'
                expr={[TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($_.LastWriteTime, 'Eastern Standard Time')}
           } |
    export-csv \corvette\c\fsync\LeftTemp.txt                                                 
}

Joe,
Welcome to the forum. :wave:t3:

before we proceed … 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

Guide to Posting Code - Redux <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

… and … that is an issue? … why? What problem are you trying to solve actually?

I want to compare the LastWriteTime between the remote machine file and a local machine to determine if the file has been changed.

LLP - Thanks for the reply - works perfect.

1 Like

@jbm

BTW: You could use the Property LastWriteTimeUtc on both sides to avoid the need for any conversion. :point_up: :wink:

2 Likes