PowerShell. I sometimes need to use it. Whenever I use it, I have to google it to know how to do it.
This time, I needed to create a PowerShell script to install msi file silently.
msiexec with relative path does not work
This is what I did at first.
# This does not work
Start-Process msiexec.exe -Wait -ArgumentList "/I .\Something.msi /quiet"
Start-Process with Wait option is used here to wait for the process completion.
But the target application was not installed in the system. It turned out that the relative path can’t be passed here.
How to get the current path
To make it the absolute path, the current path is needed. I tried to use Get-Location first.
$current = Get-Location
$joinedPath = Join-Path $current "Something.msi"
Write-Host $joinedPath
But Get-Location returns the current directory path but not the path for the PowerShell script. If the execution directory is different from the directory where the PowerShell script exists, the path becomes the non-desired path. So this way is not good.
A better way is to use $PSScriptRoot
that returns a path to the currently executed PowerShell script.
$absolutePathToMsi = Join-Path $PSScriptRoot "Something.msi"
Write-Host $absolutePathToMsi
We can generate the desired path in this way because we already know where the target file exists. If the file exists in bins
directory, the code will be the following.
$absolutePathToMsi = Join-Path $PSScriptRoot "/bins/Something.msi"
Write-Host $absolutePathToMsi
The final code to execute msi silently
$absolutePathToMsi = Join-Path $PSScriptRoot "Something.msi"
Start-Process msiexec.exe -Wait -ArgumentList "/I $absolutePathToMsi /quiet"
Comments