How to use the Windows PowerShell cmdlet Get-ItemProperty to work with file attributes.

Working with PowerShell’s Get-ItemProperty to get file attributes may boost your work productivity. It’s often easier to getting things done if you have a more generic way of doing something. Here is how to use Get-ItemProperty in PowerShell to perform and streamline day to day tasks like installing or updating software based on file versions.

PowerShell’s Get-ItemProperty

From Get-ItemProperty on docs.microsoft.com: 

The Get-ItemProperty cmdlet gets the properties of the specified items. For example, you can use this cmdlet to get the value of the LastAccessTime property of a file object. You can also use this cmdlet to view registry entries and their values.

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

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 itself.

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
PS D:\Users\janreilink> cd Z:\deploy\packages\dotnetcore\
PS Z:\deploy\packages\dotnetcore> If ((Get-ItemProperty .\dotnet-hosting-2.1.3-win.exe).VersionInfo.ProductVersion -ge (Get-ItemProperty .\dotnet-hosting-2.1.0-win.exe).VersionInfo.ProductVersion) {
	Write-Host "This version is newer, let's install it"
}

Psst… want to learn more about PowerShell comparison operators like -ge? See my PowerShell introduction post.

This may interest you:   IIS application pool recycle on specific times, not regular time interval

Installing software with PowerShell

Knowing these file properties, like the version numbers that I can use in scripting to compare, 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).PSPath -argument "/install /quiet" -Wait

Enjoy!

Please note: some information is copied from the Hey, Scripting Guy! Blog post Use a PowerShell Cmdlet to Work with File Attributes. I put it up here for reference.