4

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.

1
  • 1
    There is a bult in variable for this - $PSversiontable.psversion Commented Mar 26, 2021 at 8:18

1 Answer 1

4

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} } 
Sign up to request clarification or add additional context in comments.

1 Comment

Tnx I'm gonna give it a try probs use the first method

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.