Working with PowerShell's Get-ItemProperty to get file attributes may boost your productivity. It's often easier to get things done if you have a more generic way of doing something. Here is how to use Get-Item and Get-ItemProperty in PowerShell to streamline day to day tasks, like installing or updating software based on file versions. I also briefly address changing timestamps on files.
(Get-ItemProperty file.exe).VersionInfo gives you the file and product version of an installer, which you can compare in a deployment script before running Start-Process. With Get-Item you can also set properties, like a file's LastWriteTime.
PowerShell's Get-ItemProperty
From the Get-ItemProperty documentation:
The
Get-ItemPropertycmdlet gets the properties of the specified items.
For example, the LastAccessTime of a file, or registry entries and their values. And the Get-Item cmdlet gets the item at the specified location, without its contents unless you use a wildcard character (*).
I want to use this cmdlet to retrieve information about the software version, because I mostly use this to install software during deployments or updates. In my example I'll use the .NET Core 2.1 Runtime & Hosting Bundle for Windows (v2.1.3) dotnet-hosting-2.1.3-win.exe.
Get file properties using PowerShell
How to use the Windows PowerShell cmdlet Get-ItemProperty to work with file attributes. As said, I use this to install or update software during deployment or maintenance. Let's get some information about the file. The examples speak for themselves.
PS D:\Users\janreilink> Get-ItemProperty Z:\deploy\packages\dotnetcore\dotnet-hosting-2.1.3-win.exe
Directory: Z:\deploy\packages\dotnetcore
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/22/2018 9:30 AM 96297912 dotnet-hosting-2.1.3-win.exe
PS D:\Users\janreilink> Get-ItemProperty Z:\deploy\packages\dotnetcore\dotnet-hosting-2.1.3-win.exe | Format-list -Property * -Force
PSPath : Microsoft.PowerShell.Core\FileSystem::Z:\deploy\packages\dotnetcore\dotnet-hosting-2.1.3-win.exe
PSParentPath : Microsoft.PowerShell.Core\FileSystem::Z:\deploy\packages\dotnetcore
PSChildName : dotnet-hosting-2.1.3-win.exe
PSDrive : V
PSProvider : Microsoft.PowerShell.Core\FileSystem
Mode : -a----
VersionInfo : File: Z:\deploy\packages\dotnetcore\dotnet-hosting-2.1.3-win.exe
InternalName: setup
OriginalFilename: dotnet-hosting-2.1.3-win.exe
FileVersion: 2.1.13126.0
FileDescription: Microsoft .NET Core 2.1.3 - Windows Server Hosting
Product: Microsoft .NET Core 2.1.3 - Windows Server Hosting
ProductVersion: 2.1.13126.0
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language: English (United States)
BaseName : dotnet-hosting-2.1.3-win
Target :
LinkType :
Name : dotnet-hosting-2.1.3-win.exe
Length : 96297912
DirectoryName : Z:\deploy\packages\dotnetcore
Directory : Z:\deploy\packages\dotnetcore
IsReadOnly : False
Exists : True
FullName : Z:\deploy\packages\dotnetcore\dotnet-hosting-2.1.3-win.exe
Extension : .exe
CreationTime : 8/22/2018 9:30:02 AM
CreationTimeUtc : 8/22/2018 7:30:02 AM
LastAccessTime : 8/22/2018 9:30:37 AM
LastAccessTimeUtc : 8/22/2018 7:30:37 AM
LastWriteTime : 8/22/2018 9:30:26 AM
LastWriteTimeUtc : 8/22/2018 7:30:26 AM
Attributes : Archive
Here you see a lot of important information about the file, such as its PSPath:
PS D:\Users\janreilink> (Get-ItemProperty Z:\deploy\packages\dotnetcore\dotnet-hosting-2.1.3-win.exe).PSPath
Microsoft.PowerShell.Core\FileSystem::Z:\deploy\packages\dotnetcore\dotnet-hosting-2.1.3-win.exe
Version description and information that we can query:
PS D:\Users\janreilink> (Get-ItemProperty Z:\deploy\packages\dotnetcore\dotnet-hosting-2.1.3-win.exe).VersionInfo.FileDescription
Microsoft .NET Core 2.1.3 - Windows Server Hosting
PS D:\Users\janreilink> (Get-ItemProperty Z:\deploy\packages\dotnetcore\dotnet-hosting-2.1.3-win.exe).VersionInfo.ProductVersion
2.1.13126.0
Cast the version strings to [version] to compare them as versions, not as text:
PS D:\Users\janreilink> cd Z:\deploy\packages\dotnetcore\
PS Z:\deploy\packages\dotnetcore> If ([version](Get-ItemProperty .\dotnet-hosting-2.1.3-win.exe).VersionInfo.ProductVersion -gt [version](Get-ItemProperty .\dotnet-hosting-2.1.0-win.exe).VersionInfo.ProductVersion) {
Write-Host "This version is newer, let's install it"
}
You can read more about PowerShell comparison operators like -gt in my PowerShell introduction.
You can also use the CIM_DataFile class with Get-CimInstance. For example to find .NET's version:
Get-CimInstance -ClassName CIM_DataFile -Filter "Name = 'C:\\Program Files (x86)\\dotnet\\dotnet.exe'" | select -ExpandProperty Version
It outputs something like: 7.0.1223.47720.
Need the .NET Framework version instead? Here is how to determine installed .NET Framework versions.
Example: Installing software with PowerShell
Knowing these file properties, like the version numbers I can compare in scripts, I can start installing Microsoft .NET Core 2.1.3. A nice PowerShell way is to use Start-Process:
$dotnetcore = "Z:\deploy\packages\dotnetcore\dotnet-hosting-2.1.3-win.exe"
Start-Process -NoNewWindow (Get-ItemProperty $dotnetcore).FullName -argument "/install /quiet" -Wait
Install .NET Core on Windows Server 2016 and up only:
if ( [System.Environment]::OSVersion.Version -gt (New-Object System.Version "10.0" ) ) {
$dotnetcore = (Get-ItemProperty Z:\deploy\packages\dotnetcore\latest\*.exe)
Write-Host "Install $($dotnetcore.BaseName)"
Start-Process -NoNewWindow $($dotnetcore.FullName) -argument "/install /quiet" -Wait
}
Editing file properties like Last Modified Date
Using Get-Item, you can change file properties as well. Suppose you want to change a file or folder's last modified date? Here is how to change timestamps on files:
PS C:\Users\janreilink\source\dev> Get-Item .\testfile.txt | %{ $_.LastWriteTime }
Monday, October 19, 2020 8:40:13 AM
PS C:\Users\janreilink\source\dev> Get-Item .\testfile.txt | %{ $_.LastWriteTime = '2020-10-10 08:50:00' }
PS C:\Users\janreilink\source\dev> Get-Item .\testfile.txt | %{ $_.LastWriteTime }
Saturday, October 10, 2020 8:50:00 AM
What we did was: get testfile.txt's LastWriteTime property, and re-set it to a date in the past by simply assigning a new date/timestamp to that LastWriteTime property.
Enjoy!
Learn how to calculate SHA-256 checksums in PowerShell to verify file integrity.
Please note: some information is based on the Hey, Scripting Guy! blog post Use a PowerShell Cmdlet to Work with File Attributes. I put it up here for reference.