Working with file attributes in PowerShell

Working with PowerShell 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-Item and Get-ItemProperty in PowerShell to perform and streamline day to day tasks like installing or updating software based on file versions. I also address changing timestamps on files in short.
Published on Wednesday, 22 August 2018

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.

Get-Item on docs.microsoft.com:

The Get-Item cmdlet gets the item at the specified location. It doesn't get the contents of the item at the location unless you use a wildcard character (*) to request all the contents of the item.

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 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 -gt (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 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.

Example: 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

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

Learn how to calculate SHA-256 checksums in PowerShell to verify file integrity

Using Get-Item, you can change file properties as well. Suppose you want to change the 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 only assigning a date/timestamp for that LastWriteTime property.

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.