powershell psobject

Jay Thomas
Jay Thomas used Ask the Experts™
on
Hi experts.
I want to compare a file on a server with one that is newer (same name but version difference) so I am trying to compare the files version, and if the same version do nothing, if older install newer file.
I have been looking at psobject -property but to be honest PS is not my strength, if the file resides :
\\SERVER1\files\myfile   and the version is 1.
my new file is also called myfile and it's version is version 2. can anyone give me the command I would use please? I'm not one for trying this site everything a problem occurs but as said PS isn't me and it could take me hours to get the answer through googling.
thank you all.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
Commented:
How is the version of the file designated?  Does it show in the file's versioninfo like
gi \\SERVER1\files\myfile | Select -expand VersionInfo | Select FileVersion

Open in new window


Depending on the answer you may be looking at different ways of comparing a file, like comparing dates, file hashes, etc.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
The correct way to do it depends on the way your actual file defines the version string.
If it's the usual convention of a maximum of 4 int32 separated by '.', it's not difficult.
$OldFile = Get-Item -Path '\\SERVER1\files\myfile'
$NewFile = Get-Item -Path '\\SERVER1\NewFiles\myfile'
If ([version]$OldFile.VersionInfo.FileVersion -lt [version]$NewFile.VersionInfo.FileVersion) {
	Copy-Item -Path $NewFile.FullName -Destination $OldFile.FullName -Force -WhatIf
}

Open in new window

Or if the file date will reflect older/newer:
$OldFile = Get-Item -Path '\\SERVER1\files\myfile'
$NewFile = Get-Item -Path '\\SERVER1\NewFiles\myfile'
If ($OldFile.LastWriteTime -lt $NewFile.LastWriteTime) {
	Copy-Item -Path $NewFile.FullName -Destination $OldFile.FullName -Force -WhatIf
}

Open in new window

Author

Commented:
thank you very much. and I can save that as a script and run from schedule task?
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Author

Commented:
hi@fotech. sorry I'm on a mobile and only saw the second response through my screen. it checks for versions.
Top Expert 2014

Commented:
??
it checks for versions.
I don't know what that means.

Author

Commented:
Hi. first of it looks like my responses are appearing more than once, could be the new EE app I'm using I have no idea. To clarify further as I realise my version answer was a bit vague. so, we look are looking at the file properties, details tab, and "product version" property.
thank you chaps.
Top Expert 2014

Commented:
oBdA's should work for you then.  Just change FileVersion to ProductVersion.

Author

Commented:
Hi both still looking at this. What one liner can I use to pull the file version info for a file? I just want to be able to test and check the version outside of a script.
Thank you.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
(Get-Item 'C:\Windows\explorer.exe').VersionInfo.ProductVersion

Open in new window

Author

Commented:
Brilliant as always. Hope point share seems fair.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial