The Two Step Guide to Upgrading to PowerShell 5.1

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

PowerShell is the scripting language of choice for sysadmins, developers and anyone else interested in automation. This tutorial will help you install PowerShell 5.1 on your Windows 10 machine, or upgrade an existing installation to this latest version

The Two Step Guide to Upgrading to PowerShell 5.1

If you do decide to install PowerShell 5.1, I’ve written a technical how-to that includes scripts to help you automate the process. Disclaimer: The code in this article is provided “as is.” Some may or may not work in your context, but if you don’t have anything else to work with, it will serve as a starting point.

If you’re having trouble figuring out what version of PowerShell you’re using, see How to Check Your PowerShell Version (All the Ways!).

The first step is to make a discovery.

The discovery process is the first step in conducting an enterprise-wide PowerShell update. What is the present state of affairs? You can’t alter anything until you first comprehend it, and many organizations, unexpectedly, have no clue what they have!

Operating Systems Currently in Use

The first step is to figure out which operating systems you presently have and want to upgrade to PowerShell 5.1 on. Although, on certain systems, you may have already chosen to install PowerShell 5.1 without realizing it. PowerShell 5.1 was included in the Anniversary Update for Windows 10 and the RTM version of Windows Server 2016, so if you have one of those operating systems or newer, you’re all set.

Make a list for the rest of the people. If you don’t have a system management program like SCCM, you’re in luck. It’s no issue! Help PowerShell by using PowerShell! Here’s a fast one-liner to obtain a bird’s eye perspective of what’s out there if you have Active Directory.

PS> Get-ADComputer -Filter “OperatingSystem -like ‘Windows*’” -Properties OperatingSystem | group operatingsystem | sort name Count Name Group —– —- —– 922 Windows 10 Enterprise XXXXXXXXXXXXXXXXXX 714 Windows 7 Enterprise XXXXXXXXXXXXXXXXXX 23 Windows 7 Professional XXXXXXXXXXXXXXXXXX 4 Windows 7 Ultimate XXXXXXXXXXXXXXXXXX 2 Windows 8 Consumer Pre… XXXXXXXXXXXXXXXXXX 1 Windows 8 Enterprise XXXXXXXXXXXXXXXXXX

What is the state of your surroundings? Do you have any computers running Windows XP or Server 2003? If that’s the case, you’re out of luck. PowerShell v2 is the best you can do there, but if you still have any of them in your environment, PowerShell is the last thing on your mind! PowerShell 5.1 may be installed on any operating system that has Windows 7 SP1/Windows Server 2008R2 SP1 or above.

Creating a Target List of Prerequisites

PowerShell 5.1 has a few prerequisites and conditions that must be met before it can be used on supported operating systems.

  • At the very least,.NET Framework 4.5.2 must be installed. This isn’t a required for installation, and the installation will still continue, but certain functions will be unavailable, so do it now!
  • It is not necessary to install PowerShell 5.1 Preview.
  • If you have PowerShell v3 installed on a Windows 7 or Server 2008R2 computer, you must either store the value of PSModulePath or update to v4 first.
  • If you’re utilizing DSC on a Windows 7 or Server 2008R2 computer, make sure WinRM is turned on.
  • Because the installation will need a reboot, be sure to plan it.

Because I’m lazy, and I want you to be too, here’s a little script I made to help you figure out what type of prereq work you have ahead of you. You might insert code from this script in there for the WinRM requirement if you’re touching each machine. However, keep in mind that this method necessitates the use of PowerShell Remoting on each PC.

$computers = Get-AdComputer -Filter *foreach ($c in $computers.Name)$computers = Get-AdComputer -Filter *foreach ($c in $computers.Name)$computers = Get-AdComputer -Filter *foreach ($c in $computers.Name)$computers = Get-AdComputer -Filter $icmParams = @ Session = $session $session = New-PSSession -ComputerName $c $icmParams = @ Session = $session @ $output = $c $c $c $c $c $c $c $c $c $c $c $c $ ## If Powerhell v3 $icmParams is being used. $env:PSModulePath; [Environment]::GetEnvironmentVariable(“PSModulePath”, “Machine”) ScriptBlock = $env:PSModulePath; [Environment]::GetEnvironmentVariable(“PSModulePath”, “Machine”) $output. PSModulePath = (Invoke-Command @icmParams) -split ‘;’ | Select-Object -Unique | Sort-Object | PSModulePath = (Invoke-Command @icmParams) -split ‘;’ ## $icmParams is the current version. ScriptBlock = $PSVersionTable.BuildVersion.ToString() ScriptBlock = $PSVersionTable.BuildVersion.ToString() ScriptBlock = $PSVersionTable.BuildVersion. $output. @icmParams PSModulePath = Invoke-Command ## If (Get-ChildItem -Path “$cc$windowsMicrosoft.NETFramework” -Directory | Where-Object

If you do decide to install PowerShell 5.1, I’ve written a technical how-to that includes scripts to help you automate the process. Disclaimer: The code in this article is provided “as is.” Some may or may not work in your context, but if you don’t have anything else to work with, it will serve as a starting point.

If you’re having trouble figuring out what version of PowerShell you’re using, see How to Check Your PowerShell Version (All the Ways!).

The first step is to make a discovery.

The discovery process is the first step in conducting an enterprise-wide PowerShell update. What is the present state of affairs? You can’t alter anything until you first comprehend it, and many organizations, unexpectedly, have no clue what they have!

Operating Systems Currently in Use

The first step is to figure out which operating systems you presently have and want to upgrade to PowerShell 5.1 on. Although, on certain systems, you may have already chosen to install PowerShell 5.1 without realizing it. PowerShell 5.1 was included in the Anniversary Update for Windows 10 and the RTM version of Windows Server 2016, so if you have one of those operating systems or newer, you’re all set.

Make a list for the rest of the people. If you don’t have a system management program like SCCM, you’re in luck. It’s no issue! Help PowerShell by using PowerShell! Here’s a fast one-liner to obtain a bird’s eye perspective of what’s out there if you have Active Directory.

PS> Get-ADComputer -Filter “OperatingSystem -like ‘Windows*’” -Properties OperatingSystem | group operatingsystem | sort name Count Name Group —– —- —– 922 Windows 10 Enterprise XXXXXXXXXXXXXXXXXX 714 Windows 7 Enterprise XXXXXXXXXXXXXXXXXX 23 Windows 7 Professional XXXXXXXXXXXXXXXXXX 4 Windows 7 Ultimate XXXXXXXXXXXXXXXXXX 2 Windows 8 Consumer Pre… XXXXXXXXXXXXXXXXXX 1 Windows 8 Enterprise XXXXXXXXXXXXXXXXXX

What is the state of your surroundings? Do you have any computers running Windows XP or Server 2003? If that’s the case, you’re out of luck. PowerShell v2 is the best you can do there, but if you still have any of them in your environment, PowerShell is the last thing on your mind! PowerShell 5.1 may be installed on any operating system that has Windows 7 SP1/Windows Server 2008R2 SP1 or above.

Creating a Target List of Prerequisites

PowerShell 5.1 has a few prerequisites and conditions that must be met before it can be used on supported operating systems.

  • At the very least,.NET Framework 4.5.2 must be installed. This isn’t a required for installation, and the installation will still continue, but certain functions will be unavailable, so do it now!
  • It is not necessary to install PowerShell 5.1 Preview.
  • If you have PowerShell v3 installed on a Windows 7 or Server 2008R2 computer, you must either store the value of PSModulePath or update to v4 first.
  • If you’re utilizing DSC on a Windows 7 or Server 2008R2 computer, make sure WinRM is turned on.
  • Because the installation will need a reboot, be sure to plan it.

Because I’m lazy, and I want you to be too, here’s a little script I made to help you figure out what type of prereq work you have ahead of you. You might insert code from this script in there for the WinRM requirement if you’re touching each machine. However, keep in mind that this method necessitates the use of PowerShell Remoting on each PC.

## This could be Active Directory, a text file, a SQL database, whatever$computers = Get-AdComputer -Filter *foreach ($c in $computers.Name) { try { $session = New-PSSession -ComputerName $c $icmParams = @{ Session = $session } $output = @{ ComputerName = $c } ## In case they’re running Powerhell v3 $icmParams.ScriptBlock = { $env:PSModulePath; [Environment]::GetEnvironmentVariable(“PSModulePath”, “Machine”) } $output.PSModulePath = (Invoke-Command @icmParams) -split ‘;’ | Select-Object -Unique | Sort-Object ## Grab the existing version $icmParams.ScriptBlock = { $PSVersionTable.BuildVersion.ToString() } $output.PSModulePath = Invoke-Command @icmParams ## Check .NET Framework 4.5.2 if (Get-ChildItem -Path “$cc$windowsMicrosoft.NETFramework” -Directory | Where-Object {$_.Name -match ‘^v4.5.2.*’ }) { $output.DotNetGood = $true } else { $output.DotNetGood = $false } [pscustomobject]$output } catch { } finally { Remove-PSSession -Session $session -ErrorAction Ignore }

Check to see whether your current scripts will work with PowerShell 5.1.

Despite the fact that members of the PowerShell team have assured me that all scripts should function with PowerShell 5.1, this isn’t always the case. Installing 5.1 on the same operating system on another computer and going through the testing that way is always a good idea for any code that’s executing essential business processes.

Deployment is the second step.

After you’ve completed all of the prerequisites and are ready to start rolling out PowerShell 5.1, the following step is to actually execute it! The process of deploying 5.1 is identical to those of previous software deployments. The method you use will be determined by the tools you currently have. If you have Active Directory and want to utilize GPOs, for example, you could do so; SCCM would function similarly. I’m trying to be product neutral here, so this is how I’d do it using PowerShell.

First, I’ll create a folder named C:PowerShellDeployment on my local system. After that, I’ll download the relevant version(s) into that folder and, while I’m at it, get a copy of psexec. Because PowerShell can’t update itself, we’ll have to resort to good ol’ VBScript. Here’s a little script I made a long time ago to get you started. I’ll store it as PowerShellDeploymentinstallPs.vbs at C:PowerShellDeploymentinstallPs.vbs.

Set oShell = WScript.CreateObject(“WScript.Shell”) Set oFso = CreateObject(“Scripting.FileSystemObject”) strWorkingDir = oFso.GetParentFolderName(wscript.ScriptFullName) ‘Change this to whatever file name it is psInstallerPath = strWorkingDir & “Windows6.1-KB2819745-x86-MultiPkg.msu” Set swbemLocator = CreateObject(“WbemScripting.SWbemLocator”) Set swbemServices = swbemLocator.ConnectServer(“.”, “RootCIMV2”) if oFSO.GetFileVersion(“c:windowssystem32windowsPowerShellv1.0PowerShell.exe”) = “6.0.6002.18111” then Set colArchs = swbemServices.ExecQuery(“SELECT SystemType FROM Win32_ComputerSystem”,,48) For Each objArch in colArchs if InStr(objArch.SystemType,”x64-based PC”) > 0 Then oShell.Run “wusa.exe ” & psInstallerPath & ” /quiet /norestart”,0,True Else Wscript.Quit(10) End If Next End if

Create a PowerShell script like this and store it someplace after you’ve saved the VBS to your local workstation.

## Get-AdComputer -Filter *foreach ($Computer in $Computers) doesn’t have to be AD$computers = Get-AdComputer -Filter *foreach ($Computer in $Computers) doesn’t have to be AD$computers = Get-AdComputer -Filter * $Computer -Quiet -Count 1) if (Test-Connection -Computername $Computer) $folderPath = ‘C:PowerShellDeployment’ Copy-Item -Path $folderPath -Destination “$Computerc$” psexec $Computer cscript “$folderPathinstallPs.vbs” Remove-Item “$Computerc$PowerShellDeployment” $Computer -Force $Computer -Force $Computer -Force $

When you run the following script, it should check to see whether the machine is online, transfer the PowerShell installation and VBS to the remote computer, run it, clean up, and restart the computer. Schedule this in a maintenance window, as usual! PowerShell installation requires a reboot, which this will accomplish without hesitation.

.Name -match ‘v4.5.2.*’) $output.DotNetGood = $true if (Get-ChildItem -Path “$cc$windowsMicrosoft.NETFramework” -Directory | Where-Object finally Remove-PSSession -Session $session -ErrorAction Ignore $output [pscustomobject]$output $output $output $output $output $output $output $output $output $output $output $output $output $output $output $output $output $output $output $output $output $

Check to see whether your current scripts will work with PowerShell 5.1.

Despite the fact that members of the PowerShell team have assured me that all scripts should function with PowerShell 5.1, this isn’t always the case. Installing 5.1 on the same operating system on another computer and going through the testing that way is always a good idea for any code that’s executing essential business processes.

Deployment is the second step.

After you’ve completed all of the prerequisites and are ready to start rolling out PowerShell 5.1, the following step is to actually execute it! The process of deploying 5.1 is identical to those of previous software deployments. The method you use will be determined by the tools you currently have. If you have Active Directory and want to utilize GPOs, for example, you could do so; SCCM would function similarly. I’m trying to be product neutral here, so this is how I’d do it using PowerShell.

First, I’ll create a folder named C:PowerShellDeployment on my local system. After that, I’ll download the relevant version(s) into that folder and, while I’m at it, get a copy of psexec. Because PowerShell can’t update itself, we’ll have to resort to good ol’ VBScript. Here’s a little script I made a long time ago to get you started. I’ll store it as PowerShellDeploymentinstallPs.vbs at C:PowerShellDeploymentinstallPs.vbs.

Set oShell = WScript.CreateObject(“WScript.Shell”) Set oFso = CreateObject(“Scripting.FileSystemObject”) strWorkingDir = oFso.GetParentFolderName(wscript.ScriptFullName) ‘Change this to whatever file name it is psInstallerPath = strWorkingDir & “Windows6.1-KB2819745-x86-MultiPkg.msu” Set swbemLocator = CreateObject(“WbemScripting.SWbemLocator”) Set swbemServices = swbemLocator.ConnectServer(“.”, “RootCIMV2”) if oFSO.GetFileVersion(“c:windowssystem32windowsPowerShellv1.0PowerShell.exe”) = “6.0.6002.18111” then Set colArchs = swbemServices.ExecQuery(“SELECT SystemType FROM Win32_ComputerSystem”,,48) For Each objArch in colArchs if InStr(objArch.SystemType,”x64-based PC”) > 0 Then oShell.Run “wusa.exe ” & psInstallerPath & ” /quiet /norestart”,0,True Else Wscript.Quit(10) End If Next End if

Create a PowerShell script like this and store it someplace after you’ve saved the VBS to your local workstation.

## Get-AdComputer -Filter *foreach ($Computer in $Computers) doesn’t have to be AD$computers = Get-AdComputer -Filter *foreach ($Computer in $Computers) doesn’t have to be AD$computers = Get-AdComputer -Filter * $Computer -Quiet -Count 1) if (Test-Connection -Computername $Computer) $folderPath = ‘C:PowerShellDeployment’ Copy-Item -Path $folderPath -Destination “$Computerc$” psexec $Computer cscript “$folderPathinstallPs.vbs” Remove-Item “$Computerc$PowerShellDeployment” $Computer -Force $Computer -Force $Computer -Force $

When you run the following script, it should check to see whether the machine is online, transfer the PowerShell installation and VBS to the remote computer, run it, clean up, and restart the computer. Schedule this in a maintenance window, as usual! PowerShell installation requires a reboot, which this will accomplish without hesitation.

Table of Content