Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Windows Server Automation with PowerShell Cookbook, Fifth Edition
  • Table Of Contents Toc
  • Feedback & Rating feedback
Windows Server Automation with PowerShell Cookbook, Fifth Edition

Windows Server Automation with PowerShell Cookbook, Fifth Edition

By : Thomas Lee
4.7 (21)
close
close
Windows Server Automation with PowerShell Cookbook, Fifth Edition

Windows Server Automation with PowerShell Cookbook, Fifth Edition

4.7 (21)
By: Thomas Lee

Overview of this book

The Windows Server Automation with PowerShell Cookbook is back with a new edition, featuring over 100 PowerShell recipes that will make your day-to-day work easier. This book is designed to help you learn how to install, configure and use PowerShell 7.2 effectively. To start with, we’ll look at how to install and configure PowerShell 7.2, along with useful new features and optimizations, and show you how the PowerShell compatibility solution bridges the gap to older versions of PowerShell. We’ll also be covering a wide range of fundamental and more advanced use cases, including how to create a VM and set up an Azure VPN, as well as looking at how to back up to Azure. As you progress, you’ll explore topics such as using PowerShell to manage networking and DHCP in Windows Server, objects in Active Directory, Hyper-V, and Azure. We’ll also take a closer look at WSUS, containers and see how to handle modules that are not directly compatible with PowerShell 7. Finally, you’ll also learn how to use some powerful tools to diagnose and resolve issues with Windows Server. By the end of this PowerShell book, you’ll know how to use PowerShell 7.2 to automate tasks on Windows Server 2022 with ease, helping your Windows environment to run faster and smoother.
Table of Contents (17 chapters)
close
close
15
Other Books You May Enjoy
16
Index

Building PowerShell 7 Profile Files

Profile files are PowerShell scripts that PowerShell runs at startup. They are easy to create and support a range of deployment scenarios. They enable you to customize your PowerShell environment. See this article on Microsoft’s PowerShell Community blog for more details on PowerShell profile files: https://devblogs.microsoft.com/powershell-community/how-to-make-use-of-powershell-profile-files/.

In this recipe, you examine profile files, download a sample PowerShell profile file, and install it on SRV1. This profile is just for the console. In a later recipe, you install VS Code and create a VS Code-specific profile.

Getting ready

You run this recipe on SRV1 after you have installed PowerShell 7. You should begin this recipe by opening up a PowerShell 7 console.

How to do it...

  1. Discovering the profile filenames
    $ProfileFiles = $PROFILE |  Get-Member -MemberType NoteProperty
    $ProfileFiles | Format-Table -Property Name, Definition
    
  2. Checking for the existence of each PowerShell profile file
    Foreach ($ProfileFile in $ProfileFiles){
      "Testing $($ProfileFile.Name)"
      $ProfilePath = $ProfileFile.Definition.split('=')[1]
      If (Test-Path -Path $ProfilePath){
        "$($ProfileFile.Name) DOES EXIST"
        "At $ProfilePath"
      }
      Else {
        "$($ProfileFile.Name) DOES NOT EXIST"
      }
      ""
    }
    
  3. Discovering a Current User/Current Host profile
    $CUCHProfile = $PROFILE.CurrentUserCurrentHost
    "Current User/Current Host profile path: [$CUCHPROFILE]"
    
  4. Creating a Current User/Current Host profile for the PowerShell 7 console
    $URI = 'https://raw.githubusercontent.com/doctordns/PacktPS72/master/' +
           'scripts/goodies/Microsoft.PowerShell_Profile.ps1'
    New-Item $CUCHProfile -Force -WarningAction SilentlyContinue |
       Out-Null
    (Invoke-WebRequest -Uri $URI).Content |
      Out-File -FilePath  $CUCHProfile
    
  5. Exiting from the PowerShell 7 console
    Exit
    
  6. Restarting the PowerShell 7 console and viewing the profile output at startup
    Get-ChildItem -Path $PROFILE
    

How it works...

In step 1, you use the $Profile built-in variable to obtain the filenames of the four profile files in PowerShell, with output like this:

Figure 1.19: Obtaining the PowerShell profile filenames

In step 2, you check to see which, if any, of the four profiles exist, with output like this:

Figure 1.20: Checking for the existence of the profile files

The profile file most IT pros use is the Current User/Current Host profile (aka $Profile). In step 3, you discover the filename for this profile file, with the following output:

Figure 1.21: Viewing the name of the Current User/Current Host profile file

In step 4, you download a sample PowerShell console profile file from GitHub. This step creates no output. After making a new profile file, in step 5, you exit PowerShell 7. After restarting the console, in step 6, you view the details of this profile file. The output from this step looks like this:

Figure 1.22: Viewing the name of the Current User/Current Host profile file

There’s more...

In step 1, you view the built-in profile filenames. As you can see, PowerShell has four profile files you can use. These files enable you to configure a given PowerShell host or all hosts for one or all users. As you can see in step 2, none of the four profile files exist by default.

In step 4, you create the Current User/Current Host profile file based on a code sample you download from GitHub. This profile file is a starting point and demonstrates many things you can do in a profile file.

In step 6, you view the profile file you created earlier. Also, notice that the prompt has changed – the current working directory when you start PowerShell is now C:\Foo.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY