Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for a powershell version

Tags:

powershell

Hi I currently have a very basic PowerShell script that runs when a computer is installed with Windows (any version). This script is below

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072

Write-Output "Installing OS Updater Modules Please Wait...."
Write-Output " "
Install-PackageProvider -Name NuGet -Force
Install-Module -Name PSWindowsUpdate -Force
Write-Output " "
Write-Output "Installing Updates...."
Write-Output " "
Install-WindowsUpdate -AcceptAll -Install -IgnoreReboot | Out-File "c:\temp\WindowsUpdate.log" -force

As I said very simple code this will install a few modules then update windows with any updates, however, it only works with PowerShell 5 and above is there a way I can check what version is installed then carry on with the script if it is PowerShell 4 and below stop executing the script.

I know it's possible to install Windows Management Framework 5.1 into the install.wim/esd which will get around this but for people who don't have this, I would like the script to stop and not have any errors.

like image 978
Negg Avatar asked Oct 11 '25 16:10

Negg


1 Answers

u can use cmdlet named "get-host", it works in ps version >=2.0

if ((get-host).version.major -le 4)
{
 #code
}
else
{
 #code
}

or if u need exact match , u can use switch construction

switch ((get-host).version.major)
{        
    4 {#installcode4}
    5 {#installcode5}
    7 {#installcode7}      
}
like image 58
FletcherF1 Avatar answered Oct 14 '25 20:10

FletcherF1