Coffee Break 13 – Checking file properties and copying files with Windows PowerShell

This time, we will look at a couple of ways you can use Windows PowerShell to handle files. Either for the purpose of checking file versions without having to ask users, or as an example here we will look at some ideas for applying an update automatically. Please note that the intention with this post is not to provide you with a tool or process of deploying updates, but to get you familiar with the potential of Windows PowerShell using some real-life scenario (as real as they get from a Support perspective).

Check file version 

To read file properties in order to find out which build and version of the Dynamics NAV Windows client is installed, we can create a FileSystemObject and get the version with these commands:

$fso = New-Object -ComObject "Scripting.FileSystemObject"
$fso.GetFileVersion("${env:ProgramFiles(x86)}\Microsoft Dynamics NAV\80\RoleTailored Client\Microsoft.Dynamics.Nav.Client.exe")

Or, we can do the same in a simpler way:

Get-Item "${env:ProgramFiles(x86)}\Microsoft Dynamics NAV\80\RoleTailored Client\Microsoft.Dynamics.Nav.Client.exe” | % VersionInfo | % ProductVersion

Or in the case that the NAV client is not installed in program files:

(Get-ItemProperty  'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Microsoft Dynamics NAV\80\RoleTailored Client').Path # Or equivalent for a 32-bit machine


Now let's create a PSSession object so we can remote into it and run the check on remote user(s) machine(s). Note that the actual script we run is exactly the same as shown above - just run in a PSSession.

$RemoteMachineName = "UsersMachine"
$PSSession = New-PSSession $RemoteMachineName

Invoke-Command -Session $PSSession -ScriptBlock{Get-Item "${env:ProgramFiles(x86)}\Microsoft Dynamics NAV\80\RoleTailored Client\Microsoft.Dynamics.Nav.Client.exe” | % VersionInfo | % ProductVersion}

For more details about Remoting, including extending remoting to a VM in the Azure cloud, see the Windows PowerShellRemoting coffee break.

Download and unzip latest cumulative update

If you don't use the Dynamics NAV image in the Azure marketplace, check this blog for the latest cumulative update. For example, choose the "NAV 2015" tag as in this URL:

In this example, we have downloaded Cumulative Update 7 (483510_intl_i386_zip.exe) to disk.

$UpdateFile = "C:\Updates\CU7\483510_intl_i386_zip.exe"

Our downloads are stamped with an extra stream, so in case Microsoft is not in your list of trusted sites, remember to unblock it (or the next steps will fail):

Unblock-File $UpdateFile

For tips on how to (un)zip files using PowerShell, and many other useful tips and scripts, check the excellent PowerShell blog: Hey, Scripting Guy!Blog 

To unzip files, we'll use the archiving capabilities delivered with .NET framework 4.5 (this will not work with earlier .NET Versions). You can find more details about this library here:

To load the assembly, run this command: 

Add-Type -assembly ""
$Destination = "C:\temp\Hotfix"



This zip file contains another zip file, so extract that in the same way

$UpdateFile = "C:\temp\Hotfix\"

Check file date stamps

Find the date stamp of your Microsoft.Dynamics.Nav.Client.exe (of course this can also be remoted):

$lastupdate = Get-Item -path "${env:ProgramFiles(x86)}\Microsoft Dynamics NAV\80\RoleTailored Client\Microsoft.Dynamics.Nav.Client.exe" | % LastWriteTime


Or check a whole folder:

$lastupdate = (get-childitem -path "${env:ProgramFiles(x86)}\Microsoft Dynamics NAV\80\RoleTailored Client").LastWriteTime  
$lastupdate #Notice that this is now an array of dates as opposed to a single date so we pipe it

$lastupdate = $lastupdate | Sort-Object DateTime -Descending | Select-Object -Last 1

Copy newer files from the Cumulative Update

Find newer files:

$UpdateFolder = "C:\temp\Hotfix\RoleTailoredClient\program files\Microsoft Dynamics NAV\80\RoleTailored Client\"
get-childitem  -path $UpdateFolder   –recurse | where-object lastwritetime -gt $lastupdate


And finally pipe them to the destination folder:

$ClientFolder = "${env:ProgramFiles(x86)}\Microsoft Dynamics NAV\80\RoleTailored Client"

or use the absolute path (and equivalent for each component):

$ClientFolder = (Get-ItemProperty  'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Microsoft Dynamics NAV\80\RoleTailored Client').Path

 #Notice that the above is the path of NAV Client installed on a x64 machine).

get-childitem  -path $UpdateFolder  | where-object {$_.lastwritetime -gt $lastupdate} | Copy-Item -Destination $ClientFolder


There are lots of things we haven't considered here, like what if the client is in use, how to handle the rest of the update (Microsoft Dynamics Nav Server and so on), but again, this is not meant as a tool or a step-by-step process, merely an illustration of some of the ways we have for manipulating files, using PowerShell.



Jasminka Thunes, Escalation Engineer Dynamics NAV EMEA

Lars Lohndorf-Larsen, Escalation Engineer Dynamics NAV EMEA

Bas Graaf, Senior Software Engineer Dynamics NAV



Comments (1)

  1. Jamie B says:

    Watch out for longer paths.

Skip to main content