Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Windows Server Automation with PowerShell Cookbook, Fifth Edition

You're reading from   Windows Server Automation with PowerShell Cookbook, Fifth Edition Powerful ways to automate, manage, and administrate Windows Server 2022 using PowerShell 7.2

Arrow left icon
Product type Paperback
Published in Jan 2023
Publisher Packt
ISBN-13 9781804614235
Length 714 pages
Edition 5th Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Thomas Lee Thomas Lee
Author Profile Icon Thomas Lee
Thomas Lee
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Installing and Configuring PowerShell 7 FREE CHAPTER 2. Managing PowerShell 7 in the Enterprise 3. Exploring .NET 4. Managing Active Directory 5. Managing Networking 6. Implementing Enterprise Security 7. Managing Storage 8. Managing Shared Data 9. Managing Printing 10. Exploring Windows Containers 11. Managing Hyper-V 12. Debugging and Troubleshooting Windows Server 13. Managing Windows Server with Window Management Instrumentation (WMI) 14. Managing Windows Update Services 15. Other Books You May Enjoy
16. Index

Installing PowerShell 7

As mentioned, PowerShell 7 is not installed in Windows by default, at least not at the time of writing. The PowerShell team made PowerShell 7.1 available from the Microsoft Store, which is useful to install PowerShell 7.1 or later on Windows 10/11 systems. Windows Server does not support the Microsoft Store.

You have other methods of installing PowerShell 7 on your systems. The first option is to use the Install-PowerShell.ps1 script, which you download from GitHub, as shown in this recipe. You can also use this recipe on Windows 10 hosts. This approach has the advantage of being the most up-to-date source of the latest versions of PowerShell.

Getting ready

This recipe uses SRV1, a Windows Server workgroup host. There are no features of applications loaded on this server (yet).

You can use either the Windows PowerShell console or the ISE for this recipe.

How to do it...

  1. Setting an execution policy for Windows PowerShell
    Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
    
  2. Updating help text for Windows PowerShell
    Update-Help -Force | Out-Null
    
  3. Ensuring the C:\Foo Folder exists
    $LFHT = @{
      ItemType    = 'Directory'
      ErrorAction = 'SilentlyContinue' # should it already exist
    }
    New-Item -Path C:\Foo @LFHT | Out-Null
    
  4. Downloading PowerShell 7 installation script from GitHub
    Set-Location -Path C:\Foo
    $URI = 'https://aka.ms/install-powershell.ps1'
    Invoke-RestMethod -Uri $URI |
      Out-File -FilePath C:\Foo\Install-PowerShell.ps1
    
  5. Viewing Installation Script Help
    Get-Help -Name C:\Foo\Install-PowerShell.ps1
    
  6. Installing PowerShell 7.2
    $EXTHT = @{
      UseMSI                 = $true
      Quiet                  = $true
      AddExplorerContextMenu = $true
      EnablePSRemoting       = $true
    }
    C:\Foo\Install-PowerShell.ps1 @EXTHT | Out-Null
    
  7. Installing the preview and daily builds (for the adventurous)
    C:\Foo\Install-PowerShell.ps1 -Preview -Destination C:\PSPreview |
      Out-Null
    C:\Foo\Install-PowerShell.ps1 -Daily   -Destination C:\PSDailyBuild |
      Out-Null
    
  8. Creating Windows PowerShell default profiles
    # First the ISE
    $URI = 'https://raw.githubusercontent.com/doctordns/PACKTPS72/master' +
           '/scripts/goodies/Microsoft.PowerShell_Profile.ps1'
    $ProfileFile    = $Profile.CurrentUserCurrentHost
    New-Item -Path $ProfileFile -Force -WarningAction SilentlyContinue |
       Out-Null
    (Invoke-WebRequest -Uri $URI -UseBasicParsing).Content |
      Out-File -FilePath  $ProfileFile
    # Now profile for ConsoleHost 
    $ProfilePath    = Split-Path -Path $ProfileFile
    $ChildPath      = 'Microsoft.PowerShell_profile.ps1'
    $ConsoleProfile = Join-Path -Path $ProfilePath -ChildPath $ChildPath
    (Invoke-WebRequest -Uri $URI -UseBasicParsing).Content |
      Out-File -FilePath  $ConsoleProfile
    
  9. Checking versions of PowerShell 7 loaded
    Get-ChildItem -Path C:\pwsh.exe -Recurse -ErrorAction SilentlyContinue
    

How it works...

In step 1, you set the execution policy for Windows PowerShell to Unrestricted. This step, which produces no output, simplifies the installation and setup of PowerShell. In production, you may wish to set PowerShell’s execution policy to be more restrictive.

Most of the scripts in this book should run successfully using a more restrictive setting. To simplify things, this recipe sets the execution policy to Unrestricted.

In step 2, you update the help text files for Windows PowerShell, which produces output like this:

Figure 1.2: Updating help files

Note that after installing PowerShell 7, PowerShell prompts you to download help text (not shown in this figure) the first time you use Get-Help.

In step 3, you create a folder, C:\Foo. This book uses this folder as a place to put files used by the book’s recipes. For example, this recipe stores the PowerShell installation file in this folder from which you execute the script to install PowerShell 7. Also, note that this step mixes spatting, using hash tables, and direct parameter specification. You can always mix and match.

With step 4, you download the PowerShell installation script from GitHub. Although you can look in C:\Foo to examine the script, this step produces no output.

The installation script is a PowerShell script. In step 5, you use Get-Help to get details on the script, as shown here:

Figure 1.3: Getting help information from the installation script

In step 6, you use the installation script to install PowerShell 7 on SRV1, with output like this:

Figure 1.4: Installing PowerShell 7

PowerShell 7 is a work in progress. On most weekdays, the PowerShell team builds updated versions of PowerShell. Monthly, the team also releases preview versions of the next major version. At time of writing, the current preview is 7.3 Preview 3 – but that should change by the time you read this and the team releases new previews. The daily and preview builds are usually very stable and allow you to try out new features that may be in the next major release. The daily build enables you to view progress on a specific bug or feature. You may find it useful to install both of these. Note that if you install preview/daily builds as shown in this recipe, you also need to ensure you keep them up to date as time goes by – Microsoft’s update services do not update these side-by-side installations.

In step 7, you install the latest preview build along with the latest build of PowerShell, which looks like this:

Figure 1.5: Installing the preview and daily builds

PowerShell, like Windows PowerShell, uses profile files to enable you to configure PowerShell each time you run it (whether in the PowerShell console or as part of VS Code).

In step 8, you download sample PowerShell profile scripts and save them locally, which produces no output. This step assumes you are running the script from the ISE – the first part creates an ISE profile while the second establishes a PowerShell profile for the Console Host.

The executable name for PowerShell 7 is pwsh.exe. In step 9, you view the versions of this file as follows:

Figure 1.6: Checking PowerShell 7 versions loaded

As you can see, there are three versions of PowerShell 7 installed on SRV1: the latest full release, the latest preview, and the build of the day.

There’s more...

In step 1, you update the execution policy for Windows PowerShell. While this simplifies the installation and configuration of hosts, it may be unduly permissive for your environment, and you can change it as needed. Don’t forget, though, PowerShell’s execution policy is not truly a security mechanism – it just slows down an inexperienced administrator. For a good explanation of PowerShell’s Security Guiding Principles, see https://devblogs.microsoft.com/powershell/powershells-security-guiding-principles/.

In step 2, you updated the help files for Windows PowerShell. This step is optional, but later steps can prompt you to update your help files if you skip it. Installing the most up-to-date help files also adds many conceptual help topics to help you get more out of PowerShell.

In step 4, you use a shortened URL to download the Install-PowerShell.ps1 script. When you use Invoke-RestMethod, PowerShell discovers the underlying target URL for the script. The short URL allows Microsoft and the PowerShell team to publish a well-known URL and then have the flexibility to move the target location should that be necessary. The target URL, at the time of writing, is https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/install-powershell.ps1.

In step 6, you use the installation script to install PowerShell 7 on SRV2. This step installs PowerShell 7.2.2, as you can see, using an MSI. The MSI, which you install silently without any user notification, updates the system execution path to add the PowerShell 7 installation folder. At the time of writing, the latest released version of PowerShell is 7.2.2. In step 7, you install the latest preview build (a foretaste of things to come in the next version of PowerShell) and the daily build (for the brave). The code here retrieves the latest supported version of PowerShell 7, plus the preview and daily builds. When you run this recipe, the versions you install are going to be later than what is shown here.

In step 8, you create two sample profile files: an ISE profile and a profile for the console host. Windows PowerShell uses the profile when you launch the PowerShell console. PowerShell 7 also uses the profile (when you run PowerShell 7 in the console or Microsoft Terminal).

In step 9, you can see that you have installed PowerShell 7 (into C:\Program Files) and the latest daily build and preview versions. The specific file versions you see may differ from the output shown here, reflecting the relentless progress of the PowerShell team.

You have been reading a chapter from
Windows Server Automation with PowerShell Cookbook, Fifth Edition - Fifth Edition
Published in: Jan 2023
Publisher: Packt
ISBN-13: 9781804614235
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime